Staic Member: You can access static member through the name of the class in which it is declared. E.g. className.StaticMethod();
You cannot access a static member through the instance of its class.
In C# there are no global methods. Static methods are similar to global method but with the advantage that they have access to local members. Equivalent of Static in VB.Net is Shared (and not Static which creates a variable that exists throughout the lifecycle of the program. Also it is not shared among different instance of the class.)
For static methods to access the non static methods of the same class, they must create instance of the class.
Static Constructor: It is guarnteed to run before the instance of the class is created. You cannot control when it will run though. No access modifiers are allowed on static contructors. You can use initializer instead of static constructor.
Static class: useful in creating small utility class holding static methods. Also it makes sure that you dont create instance of the class. Also it is a sealed class which cannot be derived.
You cannot access a static member through the instance of its class.
In C# there are no global methods. Static methods are similar to global method but with the advantage that they have access to local members. Equivalent of Static in VB.Net is Shared (and not Static which creates a variable that exists throughout the lifecycle of the program. Also it is not shared among different instance of the class.)
For static methods to access the non static methods of the same class, they must create instance of the class.
Static Constructor: It is guarnteed to run before the instance of the class is created. You cannot control when it will run though. No access modifiers are allowed on static contructors. You can use initializer instead of static constructor.
Static class: useful in creating small utility class holding static methods. Also it makes sure that you dont create instance of the class. Also it is a sealed class which cannot be derived.
Comments