Introduction to Static Members of C# ( C Sharp)
Generally any member of a class is instance specific. It means that each instance of the class has an individual copy of the member. Member value altered by one instance will not be reflected in the member value of another instance of the same class.
are situations when you need a member to be class specific instead of
instance specific. Class specific means that only one copy of the member
is maintained irrespective of how many ever instances are created. Moreover
the member will be accessible only using the class name instead of instance
However the following members of a class cannot be declared as static:
Also note that only classes and their members can be declared as static. No other types can be declared as static. In this article, you are going to discuss static class members that are mentioned above.
Static Fields: If you want any data member of the class to be class-specific rather than instance-specific then you can declare that data member of field as static. When do you really need class specific field? Consider a scenario where you have to determine how many instances have been created for your class. To determine this count, you can introduce a static field and increment its value inside the constructor.
To create an instance of your class, you will use new operator. This new operator will trigger the constructor and inside the constructor you have incremented the static field value. Hence at any point of time if you print the static field, you will know the number of instances created for that class. This logic is demonstrated in the code shown below:
Output of this code will be:
Totally 3 instances of sampleClass are created.
In this example, the static field objCount will contain the number of instances created for sampleClass. Note that if you dont mark the data member objCount as static, this logic will not work. This is because non-static data members belong to each instance as individual copies. For each instance, new operator is used once and so constructor will be called only once. Hence the data field will always contain the value 1 for any instance that is created and it has no chances of getting incremented further.
Static Methods: Static methods are used in a class to access only the static fields and static events. Non-static members of a class cannot be accessed using static methods. In the above example, the static field objCount is declared as private. Hence it cannot be directly accessed in classes other than sampleClass. However you want to access it in testClass to display the number of instances created for sampleClass.
To achieve this, you create a static method inside sampleClass which returns the static field objCount. You then call this static method inside testClass to fetch and display the value of objCount. Remember that the access modifier of the static method decides on its visibility in other classes. Since the static method getCount of sampleClass has its access modifier as public, you do not have any issue when you call it inside Main method of testClass.
Static Properties: If you want any property to be class- specific then you can declare it static. Similar to static methods, static properties can access only static fields and static events inside their accessor methods. They cannot access non-static members of the class. Given below is a simple example that demonstrates how to create and use a static property:
Output of this code will be:
accessor of static property
all other static members, you can also create static events. Syntax for
creating static event is:
Static constructors can be used inside either a static class or non-static class. They are used to initialize static fields when you load the class for the very first time. This constructor will be executed before you create any instance for the class and also before you try to access the static data fields.
Here is an example containing a static constructor which initializes the static field named var of sampleClass:
to our mailing list and receive new articles Note
: We never rent, trade, or sell my email lists to Visit
.NET Programming Tutorial Homepage ______________________________________________________
to our mailing list and receive new articles
: We never rent, trade, or sell my email lists to
Visit .NET Programming Tutorial Homepage
| How Do You Establish Polymorphism in C# (C Sharp) | How Do You Overload == Operator in C# (C Sharp) ? | How Do You Overload == Operator in C# (C Sharp) ? | How to Perform User Defined Conversions Between Structures (Struct) | Illustration of C# (C Sharp) Keywords (params, ref, out) Used to Declare | Introduction to Static Classes of C# (C Sharp) | Introduction to Static Members of C# ( C Sharp) | Overview of Structs in C# (C Sharp) | Using Constructors and Destructors in C# (C Sharp) |