Using Static Members
The members of a class (variables, methods, events, indexers, etc.) can be instance members or static members. Instance members are associated with instances of a type, whereas static members are considered to be part of the class. Thus, there is one instance member for each object instantiated, but there is always exactly one static member, no matter how many objects are created (even if no objects are created).
You access a static member through the name of the class in which it is declared. For example, suppose you have a class named Button and have instantiated two objects of that class named btnUpdate and btnDelete.[6] Suppose as well that the Button class has a static method SomeMethod( ). To access the static method, you write:
Button.SomeMethod( );
rather than:
btnUpdate.SomeMethod( );
In C#, it is not legal to access a static method or member variable through an instance, and trying to do so will generate a compiler error.
Tip
VB 6 programmers take note: don't confuse the static keyword in C# with the Static keyword in VB 6 and VB.NET. In VB, the Static keyword declares a variable that is available only to the method in which it was declared. In other words, the Static variable is not shared among different objects of its class (i.e., each Static variable instance has its own value). However, this variable exists for the life of the program, which allows its value to persist from one method call to another.
In C#, the static keyword indicates a class member. ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access