Wednesday, March 20, 2013

Static vs Instance in C#.net



-The variables, methods & constructors defined under a class are known as "Members" of a class, where the members of a class are categorized into 2 types:
    -Instance Members (Non-Static)
    -Static Members

-The members under the class which requires the object of that class for initialization or execution are known as Instance or Not-Static members.

-The members under the class which doesn't require the object of that class for initialization or execution are known as Static members.

Instance Variables Vs Static Variables:

-An Instance Variable gets initialized when the object of the class is created where as a static variable is initialized when the execution of the class starts.
-If you want to declare an static variable it can be done either by using the static modifier or variables declared under a static block is also static.
int x = 100; // Instance
static int y = 200; // Static
static void Main()
{
  int z = 300; // Static
}

-The minimum and maximum no. of execution for an instance variable is 0 or n, where as in case of a static variable it is 1 and only 1.

Note: Use an instance variable if u want the changes made by one object not to be reflected to the other, use static variable if u want to reflect the changes made by one to the other.

-We refer to instance members of a class using the object of the class where as we refer to the static members of a class using the Class Name.
-------------------------------------------------------------------------------------
Static Vs Instance Methods:
-A method declared with the help of a "static" modifier is a static method and the rest are instance.

-A static method is invoked with the name of its class. eg: WriteLine is a static method under the class Console which is referred as Console.WriteLine.

-A non-static member of a class cannot be referred from a static block direclty, but still if u want to refer this can be done with the object of the class.

Note: While defining static method never refer to the non-static members with out the object of class
-------------------------------------------------------------------------------------
Static Vs Instance Constructors:

-As we were aware constructors are responsible in initializing the variables of the class, for instance variables instance constructors and for static variables static constructors were used.

-An static constructor is called only once in the life cycle of a class i.e. when the execution of the class starts, where as instance constructors are called each time u create the object of class.

-The first block of code which executes under a class is Static Constructor.

Note: If u want to perform any action once the execution of the class starts write it under the static constructor, in the same way any actions which should be performed when ever the object is created write them under instance constructor.

-Static constructors cannot be parameterized, it can be done only for instance contructors.
-------------------------------------------------------------------------------------
class Customer
{
 double bal;
 static int count = 0;
 Customer(int Custid)
 {
  count++;
  bal = <load from db>
 }

No comments:

Post a Comment

amazon

Sukanya Samriddhi Account - SBI

SUKANYA SAMRIDDHI Account information by SBI SUKANYA SAMRIDDHI ACCOUNT : FACILITY AVAILABLE AT ALL BRANCHES OF SBI Sukanya Samriddhi ...