The members of the class (methods, properties, variables etc.) may be classified into two types: static and non-static members. There should be some means to access the class members to perform a functionality.
To access the non-static (or instance) members of the class within the same class or outside the class, we need to take permission from that class. To take permission from the class we need to create a copy of that class which is mostly referred as an object.
Eg: class Test1
{
public int a=10; //non-static member
static void Main()
{
Test1 t1=new Test1();
Console.WriteLine(t1.a); //To access non-static member 'a' within same class
// 'Test1' where it is defined requires object 't1'
//of that class.
}
}
Eg: class Test2
{
static void Main()
{
Test1 t1=new Test1();
Console.WriteLine(t1.a); //To access non-static member 'a' of class 'Test1' from class
//Test2 also requires object 't1' of the class 'Test1'.
}
}
To access the static members of the class within the same class, we can directly refer them. But to access the static members outside the class we need to use that class name under which they are defined to refer them.
Eg: class Test3
{
public static int b=20; //static member
static void Main()
{
Console.WriteLine(b);// static member 'b' defined under class 'Test3' is directly
// referred, no object of 'Test3' required.
}
}
Eg: class Test4
{
static void Main()
{
Console.WriteLine(Test3.b); //static member 'b' of class 'Test3' is consumed under class
// 'Test4' by referring it with class name 'Test3' where it is
//defined
}
}