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) is needed and shared among all instances (objects) of a type (and members).
Let's simplify it. Let's say, you have a class "CSharpCorner" that represents the 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.
Note. 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 of some simple examples and try to understand the concepts.
Static variable in C#
A variable can be declared as static.
Case-study
When we don’t want to create multiple instances of a variable, i.e., we want to access the same value across multiple instances of a class, then we can opt for a static variable.
To access static variables we don’t need to create an object of the class. It can be accessible by the class name.
Note. However, if we manipulate the static variable inside a non-static method then each time an object will be created 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 the Add2Numbers() method, the value of the static variable has been updated to 20.
Now we are creating one more object to call the PrintStaticVariable() method. After execution of the PrintStaticVariable() method, the value has been modified to 30 at all the places.
Static Methods in C#
Case Study
When we have a scenario that focuses more on 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 we also need to use the class name. No instance is required.
Q. Can a static method access non-static fields?
Ans. The answer is Yes. Static methods can directly access static fields without using class names. 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#
Case Study
When we have all member functions and data members 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 an example.
In the above example, we are getting a compile time Error. Saying cannot declare instance members in a static class.
Q. If in a static class, we don’t have nonstatic members/functions then does it hold any default constructor?
Ans. 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.
Q. Can static constructors have public/private/internal/protected internal access modifiers?
Ans. No, the static constructor doesn’t contain any access modifiers. We have already seen the implementation in the 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.
Learn more
Want to learn more about static in C#, read Working with Static in C#