Static Keyword In C#

We are more or less familiar with the atatic keyword in C#. Static keyword can be used with a class, function, variable, cor onstructor.

Let us see the use by some simple examples and try to understand the concepts.

Static variable

A variable can be declared as static.

Case-study: 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.

access static variable

Note:

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 inan example.

class

After execution of Add2Numbers() method the value of static variable has been updated to 20.

example

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.

program

Static Methods

Case Study: 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.

PrintStaticVariable

Question: Can a static method access non-static fields?

Answer

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

Case- Study: 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.

read error

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?

Answer

Yes, it does hold a constructor but it is also declared as static.

see error

Inthe  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.

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?

Answer

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.

Read more articles on C# Programming:

Up Next
    Ebook Download
    View all
    Learn
    View all