Skip to content

KarkiBindu/CSharp

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

39 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

CSharp

Understanding C# from basics with kudvenkat Tutorial Source :

  1. Namespaces :

    • A namespace is used to organize code and assists in avoiding naming clashes
    • It is a collection of types; classes, interfaces, structs, enums and delegates
    • They do not correspon to file, directory or assembly name.
    • They could be written in separate files and/or separate assemblies and still belong to the same namespace.
    • Example:
      using system; system is a namespace containing various classes
    • Aliases can be used if the name space is long.
      Example: if a class Teacher is Contained in NameSpace Eductaion.School.Employee then instead of using fully qalified name we can provide short name as:
      using SchoolEmp = Eductaion.School.Employee; so that we donot have to use long fullname each time
  2. Reading and Writing to Console :

    • Console.Writeline(srting);
      Console.Write(string);
      Console.Readline(string);
      Console.Read(string);
      are used to read and write from console
    • Read takes input from console and write provides output to console
    • Place holders' can be provided to print variable
      Example: Console.WriteLine("The full name is: {0} {1}", firstName, lastName); firstname and lastname are string arguments and {0} is placeholder for first name and {1} is placeholder for lastName
  3. Buit-in Types :

    • Boolean : represents true or false
    • Integral : numeric values without decimal
    • Floating : numeric values with decimal
    • Decimal : numeric value with decimal
    • String : character or arrays of character
  4. Escape Sequence :

    • \ is used to make quotation or \ to be readable by the program.
    • Example: To provide the file location we do "C:\\Desktop" or to print hello in quotation marks we will have to provide "\"Hello\""
    • Verbatim Literal : Verbatim literal is a string with @ symbol prefix which makes escape sequences translate as normal printable characters, to enchance readability.
      Example: instead of doing "C:\\Desktop" we can do @"C:\Desktop" which will make the code more readable
  5. Common operators :

    • Assignment Operator : "="
      • assigns value to a variable.
      • Example: int a = 10;
    • Arithmetic Operators : "+, -, *, /, %"
      • does mathematical calculation
      • Example int a = 10 % 4; it calculates the reminder of the division and assigns the result to a variable, in this case a is equal to 2
    • Comparison Operators : "==, !=, >, <, >=, <="
      • Compares the value or any condition
      • Most commonly used in if, else consitional statement and it returns true or false
      • Example :
        if (a == 10){ Console.WriteLine("True");}
        else{ Console.WriteLine("False");}
        it checks whether the value of a is 10 or not, if its true the it prints True, else it prints False
    • Conditional Operators : "&&, ||"
      • it checks logical and/or conditions
      • Example :
        if (a == 10 && a > 5){ Console.WriteLine("True");}
        else{ Console.WriteLine("False");}
        it checks whether the value of a is 10 and greater than 5 or not, if its true the it prints True, else it prints False
    • Ternary Operators : "?:"
      • It is a short form of if else condition and used if there is only one check condition
      • Example :
        bool result = (a = 10) ? true : false; ? acts as then and : as else, statement before ? is condition checking after it is the result if the condition is true and after : is the result if the condition is false
    • Null Coalescing Operator : "??"
      • There are two types of datatypes in C#; value type and reference type
      • Refernce types can be null but value type can never be null
      • Example: int is value type and string is reference type
      • To make int type nullable add j after int : i.e intj a = 10;
      • and to make sure to provide value when it is null we do int b == a ?? 0; This convert nullable int to non-nullable int and also it provides value 0 if the value of a is null
  6. Data Conversion :

    • It can be converted in two ways :
      • Implicit conversion : It is done when the conversion will not result to loss of data, or throws an exception
      • Example
        int a = 10;
        double j = a; It is possible because there will be no loss of data or throws an exception But
        double j = 10.05;
        int a = j; this is not possible because while converting it 0.05 will be lost as integer value cannot hold decimal number
    • Explicit Conversion :
      • Casting : int a = (double)j;
      • Using Convert class: int a = Convert.ToDouble(j);
  7. Parse and Try Parse :

    • If a string is in a string format we can use parse or try parse method for conversion.
    • Parsing tries to directly convert the string values to numerical and if it cannot then it will throw exception which will result in crashing
    • Whereas try parse first checks if the value can be converted or not, if it cannot be converted the it returns 0.
    • To avoid exception and crashing using try parse method is safe
    • Example
      string num1 = "50";
      string num2 = "50ab";
      we can do int a = int.Parse(num1); But int a = int.Parse(num2); will throw an exception as it cannot be converted to numerical value. Hence we use int.tryParse(num2, out a);
  8. Arrays :

    • An array is a collection of same datatypes.
    • It is strongly typed but it cannot grow in size once initiated.
    • It has to rely on integral indices to store or retrieve items from the array.
    • Example :
      int[] i = new int[3];
  9. Conditional Statements :

    • If and If Else statement :
      • Syntax:
        if(condition)
        {
        //statement
        }
        else
        {
        //statement
        }
    • Switch Statement :
      • Syntax : Switch(case)
        {
        Case 0:
        //statement
        break;
        Case 1:
        //statement
        break;
        default:
        // statement
        break;
        }
  10. Loops :

    • While Loop :
      • Syntax :
        While(Condition)
        {
        //statement
        }
      • In while loop the code enters while loop only if the condition is true and it is repeated as long as the condition is true.
    • Do While Loop :
      • Syntax :
        Do
        {
        //statement
        }While(condition);
      • The code enters the loop atleast one time whether the given condition is true or not
    • For Loop:
      • Syntax :
        For (Variable start value; variable end value; variable increment/decrement)
        {
        //statement
        }
      • It requires to know the amount of the number for the loop to execute.
    • Foreach Loop:
      • It is used for lists, arrays etc.
      • Syntax:
        foreach(variableType newName in List/ArrayName )
        {
        //statement
        }
      • It does not need to know the amount for the loop to execute
  11. Methods :

    • Methods are also called functions
    • It helps to create reusable code and helps in application maintenance
    • Syntax :
      access-modifiers return-type MethodName(parameters)
      {
      //Method body
      }
  12. Parameters in Methods :

    • Value parameters :
      • It creates a copy of the parameter passed to the method, so modification of parameter in method, does not affect the variable
      • Example:
        public int Sum (int a, int b)
        {
        return a+b;
        }
        a and b are value parameters
    • Reference parameters :
      • Ref keyword is used to initialize reference parameters in a method
      • It refers to the same variable that was paased on the method so change made to the parameter on the method will be reflected on the variable
      • Example :
        public int Sum (int a, ref int b)
        {
        b += a;
        return b;
        }
        here b is reference parameter and the value of the variable passed on call will be changed to the value of sum of a and b
    • Out parameters :
      • It is used when a method has to return more than one value
      • The parameter with out keyword will have the assigned processed value
      • Example :
        public int Sum (int a, int b, out int sum)
        {
        sum = a + b;
        return b;
        }
        , the value is returned from sum parameter
    • Parameter Arrays :
      • Arrays or list of variable can also be passed as parameter on a method
      • Example :
        public int Sum (List numbers)
        {
        return numbers[0] + numbers[1];
        }
  13. Classes :

    • It contains data and behaviors
    • Data is represented by field and behaviour is represented by methods
    • It is used to creare complex custom types.
    • Classes have constructor which are simply methods with no return type and having same name as the class
    • Constructors are used to initialize class fields and they can be overloaded by the number and types of parameter
    • Constructors are mandatory, if not provided code will genertae parameter less constructor by default
  14. Static and non-static members of class :

    • Static members have static keyword whereas non-static members does not have static keyword
    • Static members can be easily invoked using class name without using object but to invoke non-static members object of the class is required
    • Static members always remains the same but non-static members belongs to specific instance only. i.e. there will always be one static members no matter how many objects are created but there non-static members will be equal to the instance of the class
    • Static constructor are called only once during whole program run and they are called before instance constructor
  15. Inheritance :

    • It allows code reuse, which redces time and errors
    • The common fields and properties are defined in the base class and derived class can se it without defining it in it
    • Base classes are automatically instantiated before derived class
    • Parent class constructors executes before child class constructor
  16. Method hiding :

    • If the base class and derived class have same method name and instance of derived class is made then it will hide the base method. i.e. the method of the base class will not be called.
    • If the hiding is intentional then use new keyword for the method
    • To call the base method we can use base keyword or cast derived class to base class and invoke hidden member
  17. Polymorphism :

    • It allows to invoke derived class methods through a base class reference during runtime
    • To allow this base class must have virtual methods which are then overriden by the derived class
    • Virtual keyword indidcates that a method can be overridden in any derived class
  18. Method overriding vs Method hiding :

    • Say base and derived class both have same print function:
      Baceclass instance = new DerivedClass();
      instance.Print();
    • If the function is hidden then the print method of base class is invoked
    • If the function is overriding then method of the derived class will be invoked
  19. Function/Method Overloading :

    • It allows class to have multiple methods with the same name but with different signature.
    • Methods can be overloaded based on number of parameters, types of parameter, kind of parameters(out, ref etc)
    • Example:
      public void Sum(int a, int b,out int sum)
      {
      //body
      }
      public void Sum(doble a, double b)
      {
      }
    • Functions' cannot be pverloaded based on return type and params modifiers
  20. Encapsulation :

    • Making fields public will make lose the control of what gets assigned and returned
    • Properties are used for encapsulation
    • There are four types of properties:
      • Read/write properties: it has both get and set accesor
      • Readonly properties : it has get accesor only
      • Writeonly properties : it has set accesor only
      • Autoimplemented properties: get and set does not have statement
      • Example:
        private int _id;
        public int Id{get; set;}//auto implemented property
  21. Struct :

    • Just like classes struct can have private fields, public properties, constructors and methods
    • Structs are value type whereas classes are reference type
    • Structs cannot have destructors
    • Structs cannot have parameterless constructors
    • Structs cannot inherit from another class but can inherit from interface
    • structs cannot be sed as base to form derived class/structs
    • Syntax:
      access-modifier struct structName
      {
      //body
      }
  22. Interfaces :

    • Interfaces also contains properties, methods, delegates or events bt only declarations and no implementations
    • It is complie time error to provide implementations for any interface members
    • Interface members are public by default and the do not allow wxplicit access modifiers.
    • It cannot contains fields
    • Class oe structs derived from interface must provide implementation for all interface members
    • Instance of interface cannot be created but interface reference variable can point to a derived class similar to polymorphism described
    • I is prefixed infornt of the name of interfaces
    • If a class/struct inherits from from two interfaces with same method then the methods can be implemented explicitly to stop ambiguity
    • If both I1 and I2 interface have same print method with void return type that a class inherited then it is implemented explicitly as
      void I1.Print(){}
  23. Abstract Classes :

    • It is incomplete and hence cannot be instantiated but used as base class only
    • It cannot be sealed; i.e. stop from being derived
    • It can have implementation for some of its members unlike interface and can have fields, access modifiers
    • A non-abstract class derived from the abstract class must have all metjods implementation in it
  24. Delegates :

    • It is a reference type type-safe(i.e it holds reference to a function) function pointer
    • Syntax:
      To declare delegate: access-modifier delegate return-type DelegateName(parametersofFunction);
      To Instantiate delegate: DelegateName variableName = new DelegateName(FunctionName);
      To Call : varibaleName(parametersofFunction);
    • Signature of function and delegates must match i.e parameters of function and return type
    • It is mostly used to make framework
    • It is used to make classes flexible; a class can be modified without changing its entire logic
  25. Multicast delegates :

    • It has reference to more than one function
    • It invokes the method according to the invocation list, in same order that they are added
    • All the functions pointed by multicast delegates are invoked but if the delgates have return value then the value of the last invoked method is returned
    • + or += registers a method to a delegate
    • - or -= unregisters a method to a delegate
  26. Exception handling :

    • An unforseen error that occurs while running program is called exception
    • If such errors are not handled then program will crash and stop working
    • We can use try, catch, finally to handle these errors
    • Syntax:
      try
      {
      // it has code that can cause error
      }
      catch(Exception e)
      {
      //code to handle thrown exception
      }
      finally
      {
      //code to clean and free resources
      }
    • finally block is optional
    • Pre-defiend specific exception must be handled first before general exception
    • Inner Exception: it returns the instance that caused the current exception
    • Custom Exceptions can also be created
  27. Enum :

    • It is strongly typed value type constant
    • Default underlying type of enum is int
    • Default value for first element is zero and gets incremented by 1
    • Its values can be customized
  28. Access Modifiers :

    • Types and Type members : classes, structs, enums, delegates, interfaces are types whereas the fields, properties, methods, constructors contained inside types are called type members
    • types can have only public and internal modifiers whereas type members can have all modifiers
    • private : available within the containing type
    • public :available anywhere, no restrictions
    • protected : available within contained and derived types
    • internal : available anywhere within the containing assembly
    • protected internal : available to any code within which it is declared or from derived types of another assembly
  29. Early binding vs Late binding :

    • Early binding means access/detect the methods, variables and propeties at compile time whereas late binding allows only at runtime
    • Early binding can flag errors at complie time But there is risk of run time exceptions with late binding
    • Early binding is better for perfomance and ease development
    • Late binding shold be used only when working with objects that are not available at compile time
  30. Reflection :

    • Reflection is the ability of inspecting an assemblies'metadata at runtime
    • It is used to find all types in and assembly and/or dynamically invoke methods in an assembly (i.e. late binding)
  31. Generics :

    • It allows to create methods and classes decoupled from the data types
    • It makes code type independent and increase the reusability based on datatype
    • Example :
      public static bool AreEqual( value1, value2)
      {
      return value1.Equals(value2);
      }
    • In above example T can be int, string, double etc as required and will work correctly for any of it
  32. Partial Classes :

    • It allows to divide a single class file into two or more class files
    • Example is Form.cs and Form.Designer.cs of windows form application
  33. Partial Methods :

    • Partial method is contained on partial class or structs
    • These are private by default
    • Implementation and declaration are done in seperate file else it is compile time error
    • Providing access modifier also creates compile time error
  34. Indexers :

    • It allows instance of class to be indexed just like arrays
    • Creating Indexers :
      1. Use "this" keyword to create an indexer
      2. Just like properties indexers have get and set accessors
      3. Indexers can be overloaded
    • Example
      public string this[int value]{ get; set;}
  35. Dictionary :

    • It is a collection of key, value pair
    • It is present System.Collections.Generic class
    • Syntax : Dictionary<keytype, valurtype>
  36. List :

    • It is also present in System.Collections.Generic class
    • It is a collection of same datatype
    • It grows automatically unlike arrays
  37. Sorting List of complex types :

    • Modification of IComparable interface and providing implementation for CompareTo() function
    • CompareTo() method return integer :
      1. Greater than zero : the current instance is greater than the object being compared
      2. Less than zero : the current instance is less than the value being compared
      3. Is zero : the crrent instance is equal to zero
    • Another way is to provide custom implementation using Icomparer interface
    • They can also be sorted using delegate or lambda expression
  38. Queue :

    • It is generic FIFO(First In First Out) collection class
    • Enqueue() is sed to add to the queue
    • Dequeue() is used to removed the item
    • Contains() is used to check if an item is present in queue or not
    • Peek() returns the first entered object, readonly
  39. Stack :

    • Stack is LIFO(Last In Fist Out) collection class
    • Push() is used to enter data into stack
    • Pop() is used to remove data from stack
    • Peek() returns the last entered object, read only
  40. Multi-Threading :

    • Process : process is what an operating system uses to facilitate the exection of a program by providing resources. Each process has a unique id associated with it.
    • Thread : A thread is a light weight process. Every process has atleast one thread called as main thread which executes the application code. A single process can have multiple threads.
    • Threads are used for concurrent processing utilising the multicore processor
    • Advantages :
      1. To maintain responsive User Interface
      2. To make efficient use of processor time while waiting I/O operations to complete
      3. To split large CPU bound tasks to be processed simultaneously on a machine that has multiple processor/core
    • Disadvantages :
      1. On a single processor/core multi threading can affect program negatively as there is overhead involved with context-sitching
      2. More lines of code is required to accomplish same task(without using multi threading)
      3. Multi threaded applications are difficult to write understand, debug and maintain
    • Syntax :
      1. Simple: Thread t = new Thread(functionName);
      2. Thread t = new Thread( new ThreadStart(function));
      3. Thread t = new Thread( delegate(){function(); });
    • To start a thread use t.Start();
    • To let the thread complete use t.Join();
    • Use ParameterizedThreadStart to pass data into the thread, but it is not type safe
    • To make a threa type safe; encapsulate the thread fnction and data it needs in a helper class and se the thread start delegate to execute the function
    • To retrieve data from thread callback methods can be used
  41. Thread.Join & Thread.IsAlive :

    • Join blocks the current thread and makes it wait until the thread on which join is invoked completes, timeout can also be specified for join
    • IsAlive checks whether the thread has completed is execution or not, returns true if completed else returns false
  42. Protecting shared resources :

    • Operators like --, ++ are not thread safe and its behaviour is inconsistent inside a thread
    • To solve it Either Interlocked or Lock fncitionaities are used
    • Performance wise interlocked fuunctionality is good but it has very limited options to increment, decrement, add or read for int and long only whereas lock is versatile
  43. DeadLocks :

    • While using multithreading deadlocks can occour; example: Lets say there are two resorces(r1, r2) required for thread t1 and t2 to perform certain task and t1 locked r1 and t2 locked r2, since both thread requires both resources and both resources have been locked by separate thread the task cannot be completed and deadlock occurs
    • Deadlocks can be resolved :
      1. Acquiring locks in a specific order
      2. Mutex class
      3. Monitor.TryEnter() method
  44. Async & Await :

    • In a multi threaded program one thread might hinder the performance of another thread. for example the click method of a form requires alot of processing and it is implemented using multi threads, if nothing is done then the whole form containing the button will freeze, even resizing or moving arond cannot be done in the form whle the process is running
    • To resolve it async and await can be used where async keyword will make methods asynchronous and await keyword specifies the sspension point which signals async method cannot continue above this point until the async method completes
    • Async can have multiple await but not mandatory
    • Async methods creates tasks which will be performed separately withoud hindering the main thread

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages