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.


However there 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 name.

Given below are the class members that can be declared as static:

• Static Fields
• Static Methods
• Static Properties
• Static Events
• Static Constructors

However the following members of a class cannot be declared as static:

• Indexers
• Destructors

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:

public class sampleClass {
private static int objCount;
public sampleClass() {
objCount++;
}
public static int getCount(){
return objCount;
}
}

public class testClass{
public static void Main() {
sampleClass obj1 = new sampleClass();
sampleClass obj2 = new sampleClass();
sampleClass obj3 = new sampleClass();
int count = sampleClass.getCount();
Console.WriteLine(“Totally {0} instances of sampleClass are created.”, count);
}
}

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 don’t 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:

class sampleClass {
public static int PROP {
get {
Console.WriteLine(“Inside get accessor of static property”);
}
set {
Console.WriteLine(“Inside set accessor of static property”);
}
}
}
class testClass {
public static void Main() {
sampleClass.PROP = 10;
int PROPValue = sampleClass.PROP;
}
}

Output of this code will be:

Inside set accessor of static property
Inside get accessor of static property

Static Events:

Similar to all other static members, you can also create static events. Syntax for creating static event is:
<access modifier> static event <event type> eventName();

Static Constructors:

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:

public class sampleClass {
public static int var;
static sampleClass() {
var = 10;
}
}

SetApp - 100 Apps for everything you ever wanted to do on Mac

Add to My Yahoo!


FREE Subscription

Subscribe to our mailing list and receive new articles
through email. Keep yourself updated with latest
developments in the industry.

Name:
Email:

Note : We never rent, trade, or sell my email lists to
anyone. We assure that your privacy is respected
and protected.

Visit .NET Programming Tutorial Homepage

______________________________________________________

Recommended Resource

| 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) |


“Amazon and the Amazon logo are trademarks of Amazon.com, Inc. or its affiliates.”

| Privacy Policy for www.dotnet-guide.com | Disclosure | Contact |

Copyright - © 2004 - 2019 - All Rights Reserved.