Sunday, October 25, 2015

C# : Part1 [Basics]

In this article , today we gonna look at some of the pre-defined keywords used in C#, access specifiers, value types & reference types and other basic concepts

Intro:
--> C# has been registered in   international recognized standards organisation where we can see specs

-->The C# code can be written in notepad and compiled using inbuilt compiler through command line, we can see C Sharp Compiler(csc.exe) in below path 
     C:\Windows\Microsoft.NET\Framework\v4.0.30319
We can compile our code using this compiler in following way
          C:\Windows\Microsoft.NET\Framework\v4.0.30319\csc.exe yourfile.cs
This produces executable file of your code , you can see the yourfile.exe by giving dir command and now you can exec that file by giving yourfile.exe 

We do have String[] args param in Main methods , we can pass parameters to it from command line  after your exe file

-->When Projects are added to Solution explorer each proj will be converted to an assembly i.e either as a dll or an executable file. 

Class:
They  define types which is understandable by assembly, which contains
 -->Properties,Fields,..i.e data the state an object can hold
 -->Behovior : Methods 
We can define accessibility of members in class

Objects are Instances of a type: 
We can create multiple instances, each instance holds different state, each instance has same behavior

Classes create reference types, Objects are stored on heap. Variables reference the object instance.
Default classes inherit  System.Object class

Employee e1 = new Employee();

e1 is referencing Employee object on heap but its not forever , we can again have i.e another employee object

e1 = new Employee();
e1.Name = "Sudhir";

Imp note here is we can have multiple variables referencing same object
eg:
Employee e2; //we are not initializing to new Employee object
e2=e1; // e2 is referencing same object that is created and assigned to  e1, we can check this by object.ReferenceEquals(e1, e2); which will return true as both reference same memory

e2.Name = "Paturu";

When we print e1.Name we still get "Paturu" because e1 and e2 both are referencing same object on heap

Mutable vs Immutable:

AS said most reference type objects are Mutable i.e can be changed at any point of time it will not create another object or memory on heap but where as  string is immutable even though it is reference type

string s1 = "Hi";

string s2 = "Hello";

s2=s1;   // Same as above class example when we check object.ReferenceEquals it will return true
but when s1="Sudhir"; and check now Object.ReferenceEquals of s1 & s2 it will return false because string is Immutable(its State cannot be changed) it will now reference other memory location in heap , so reference will be changed hence now s1 will reference one memory and s2 refer other 

Inheritance: Ability to define a class that inherits state and properties and behaviour from other calsses.
    This is one approach to reuse code (But not to reuse code we use inheritance )
Encapsulation : Ability to hide details inner workings of a class

Polymorphism: Plays role with inheritance to reuse code with extensibility mechanism where by customizing class that is inheriting behaviour for other class

Access Modifiers: 

Public : Can apply to class or member of a class and it creates open access , classes declared public can be  accessed from other projects implied they should be added reference

Protected : Can be applied to Members  of a class , access is limited to the class that declaring the protected member and any derived class(inherited class). Only when inherited (Can be accessed when inherited both in current assembly and outside also) but where as when we create instance of that class we cannot access it.

Internal : Can be applied to a class or Member of a class , where access is limited to current assembly .
This is default accessibility of a class in C# . i.e only internal classes can be referred inside the application or project even though this is referred in another project we cannot access such classes because it is out if assembly 

Protected Internal : Applied to members of class where there access varies in current assembly and when referred in other projects
1.When in current assembly it acts as internal i.e when we create instance of class we can access it.
2.Outside the assembly it can be accessed but only when from derived class i.e when it is inherited.i.e acts as protected outside the assembly.

private : Can be applied to members Default accessibility of a member, access limited to the class

Abstract:
This  can be applied class  and can also applied to methods, properties all members
 Abstract class cannot be instantiated , i.e we cannot use new operator to create object i.e it is designed to use it as base class , only can be inherited i.e only for reuse 
The members in base class does not have implementation jus declaration 

If abstract classes have abstract methods then such methods need to be implemented in derived class  , we can implement using a override keyword

Virtual : This  keyword creates  virtual members, where we can change the behaviour of member if required in derived class using Override keyword , while overriding we may sometimes need to implement that original definition with some extra features then we can use base.thatMethod(); inside override before your custom implementation

Static : Are members of the type, Cannot invoke the member through object instance , Cannot instantiate a static class eg: Math.Pi, Colors.Red etc  . Only one copy is maintained through out once declared.


Sealed : Sealed classes cannot be inherited (To prevent extensibility or misuse, for security purposes)
eg:  System.String is sealed class we cannot inherit it. (Improves performance)

In the same way for members: When a method is declared as virtual we can override with sealed keyword so that when this child class is inherited by some other class then we cannot override that method.

Partial :  This keyword is used to split class definition across multiple files. We can also have  partial methods just like classes , partial methods cannot have implementation jus like abstract methods. we can define them in other file of this  partial class.

Reference Types:
Multiple variables can point to same object , single variable can point to multiple objects over its lifetime i.e (Mutable ..its sate can be changed ).
--> Objects are allocated on the heap by new operator  (Value type variables will be stored on stack.)


Value Types:
Variables hold value, No pointers or references, Immutable . No objects are allocated on heap - lightweight .
Many built in primitives are value type only allowed to store less data not more than 16 bytes .. When we copy a variable x of  Int32 to variable of y Int32 then entire whole 32bits are copied which causes performance issues

Struct : These are also Value type , Like a class we add properties,fields and methods but we cannot inherits these structs and should be less than 16 bytes and ,must be primitive data types

Parameters:  In C# all parameters always pass by value (default), where as reference types pass a copy of the reference i.e changes are propagated to caller.

   eg:
class Dog
 {
    public string name { get; set; }

 }
 static void Main(string[] args)
 {
   Dog obj = new Dog();
   obj.name = "Main Obj";
   Console.WriteLine("Original :" + obj.name);
   TestingParams(obj);
   Console.WriteLine("After Calling Method : " + obj.name);
   Console.Read();
 }

public static void TestingParams(Dog objDog)
  {
    objDog.test = "Modified";

  }

Here, second time we will get o/p as Modified  even though base object isn't modified in main method.It implies for reference types params changes effect caller object as they pass reference not actual value.

Note: This is not applicable for string because its immutable as defined above.

Parameter Keywords :  For those types where we cannot change values of caller method i.e Value types we can use keywords 'ref' and 'out'

Both are used for same purpose , but major difference is  when you declare with 'ref' in the child method it will check whether that is initialized with some value in parent method because it will be used in chid method where as when declared as 'Out' no need to initialize in parent method.

eg: 
public static void Method1()
 {
   string Name;
   int Id;
   Dog obj = new Dog();
   obj.Name = "alpha";
   ChildMethod(ref Name, out Id, obj)  // Here we get obj.Name as Beta
   childMethod2(obj); // Here Obj.Name will not be Test Dog . If we like to change                      the base object then we need to pass its reference like ref obj

        }

public void childMethod(ref string Name, out int id, Dog objDog)
  {
     id = 0;    // we have to define here otherwise we get error as  out param must                     be declared before control leaves current  method   
     objDOg.Name = "Beta";

   }


public void childMethod2(Dog objDog)
 {
   objDog = new Dog(); // A new instance is declared no more points to same                     reference pointed by object in main  method
   objDog.Name = "TestDog";

 }


params:  when you like to send a set of values of same type then we declare an array and send it , where as params keyword provides flexibility to pass by values (any number dynamically)

eg : 
public void Testing(int[] values)
 {

 }

To send integers you want to play we pass integer array or something like  Testing(params int[] values);
where as when we declare method with params keyword as below we can have flexibility of passing values directly like Testing(2,3); or Testing(4,5,6,7); or Testing(9);
public void Testing(params int[] values)
 {


 }


enum:  Creates a value type set of named constants (Why : To Improve Readability)
--> Underlying datatype is  int by default  
By default value in an enumeration start with zero , however we can explicitly define the behaviour 

Arrays: Sample data structure for managing  a collection of variables , everything inside  will have same datatype , indexed from Zero . The index of item which is not in array will be '-1'.
Array inherits from many interfaces like Iclonable, IList etc so in the params instead a array variable we can also use IList or any interface (It will take any element that implement IList interface)