The static keyword in C# language is used to declare static classes and static class members. The static classes and static class members such as constructors, fields, properties, methods, and events are useful when only one copy of the object (class or class members) are needed and shared among all instances (objects) of a type (and members).
Let's simplify it. Let's say, you've a class "CSharpCorner" that represents C# Corner website, its properties, and its functions. The CSharpCorner class has four properties, Founder, YearFounded, Location, and Description. We know that all these properties are fixed and they will not change regardless of how many instances of CSharpCorner type are created by various programs or classes. This is a perfect example of a static class and static properties.
You can change static member values but the last updated value will be available to all programs or callers that call a static member.
Let us see the use by some simple examples and try to understand the concepts.
Static variable in C#
A variable can be declared as static.
When we don’t want to create multiple instance of a variable, i.e., we want to access the same value across multiple instance of class, then we can opt for a static variable.
To access static variable we don’t need to create an object of the class. It can be accessible by the class name.
However, if we manipulate the static variable inside a non-static method then each time an object will be created and the non-static method will be called and the changes to the static variable will be shared across each object created earlier.
Let’s see an example.
After execution of Add2Numbers() method the value of static variable has been updated to 20.
Now we are creating one more object to call PrintStaticVariable()
method. After execution of PrintStaticVariable()
method the value has been modified to 30 at all the places.
Static Methods in C#
When we have a scenario which focuses more into the output only without any requirement to store or retrieve data that is unique to any specific instance of a class, then we can opt for Static Methods.
To access the static methods also we need to use the class name. No instance is required.
Question - Can a static method access non-static fields?
The Answer is Yes. Static methods can directly access static fields without using class name also. However, to access non-static fields an object needs to be created. An example has been shown in the above image.
Static Class in C#
When we have all member functions and data member in a class declared as static we should declare the class as Static. So, in other words, we can say a static class should not contain any non-static fields/methods.
Let’s see in an example.
In the above example we are getting a compile time Error. Saying cannot declare instance members in a static class.
Question - If in a static class, we don’t have non static members/functions then does it hold any default constructor?
Yes, it does hold a constructor but it is also declared as static.
In the above example we are trying to create an instance constructor but during compile time we are getting an error. So let’s declare it as static.
Now we have declared the constructor as static but again at compile time we are getting one more error.
So, let's correct the error. To correct the error, we have to remove the public access modifier.
Question - Can static constructor have public/private/internal/protected internal access modifiers?
No, static constructor doesn’t contain any access modifiers. We have already seen the implementation in previous example.
Memory Management for class/methods/fields declared as Static
The type of information for a static class is loaded by the .NET Framework common language runtime (CLR) when the program that references the class is loaded. The program cannot specify exactly when the class is loaded. However, it is guaranteed to be loaded and to have its fields initialized and its static constructor called before the class is referenced for the first time in your program. A static constructor is only called one time, and a static class remains in memory for the lifetime of the application domain in which your program resides.