C# Static Keyword3 min read

Static is a keyword in C# that is when used with different to declare static classes and static class members. Static can be field, method, constructor, class, properties, operator, and event. The instance of a class is not required in order to access static members.

There is only one copy of the object is created for the static members and is shared among all the objects. Let see the use of static in three areas of the program:

  • static field
  • static method
  • static class

C# Static Variable or field

The variable or field in class when declared static is called static field. The difference between static and instance fields is, no matter how many times you create an object for the class, there will be only one memory allocated for the static field of the class and is shared by all the objects.

But in the case of the instance field, with the creation of every object of the class, a new memory is allocated for the instance field for each object.

Example: C# example for the static field.

Output:

Temp count for st1: 4
Temp count for st2: 4

As you can see, no matter which object you use to call, the temp keeps increasing. This indicated that the static field has only one instance and is used by all the other objects.


C# Static Methods

The methods that are declared with a static keyword are called static methods. This method can only access the static field of the class.

Just like a static field, the static function also does not depend on the object of the class. Let us see an example.

Example: C# example for the static method.

Output:

Temp count for st: 2

Also, note that the static method can not be invoked with the object of the class. The class name itself is used to invoke the static method. In the above eg. StaticExample.getTemp() is used to invoke the static method getTemp().


C# static class

The static class in C# is cannot be instantiated. It can only have static members in its body. And it is created using a static keyword in front of the class like the static field and method.

Following are some points to remember about static class.

  • The instance of static class cannot be created.
  • To access static members, a class name is used which is followed by the member name.
  • It only has static members and canoot be intatiated.

Syntax of static class

Example: C# example for the static class.

Output:

Side of a square: 10
Area of a square: 100


MORE

Keith Number Program in C

In this tutorial, we will write a Keith number in C. It is one of the most asked questions in an interview. Before that, you …
Read More

Keith Number Program in C++

In this tutorial, we will write a Keith number in C++. It is one of the most asked questions in an interview. Before that, you …
Read More

Singly linked list Program C++

In this tutorial, we will write a C++ program to implement singly linked list. In this program, we will create a separate function for each …
Read More

Fascinating Number Program in C

In this tutorial, we will write a C program to check whether the given number is a Fascinating number or not. You may go through …
Read More

Peterson Number in C

In this tutorial, we will learn about the Peterson number and check if the number is Peterson or not in C. What is Peterson Number? …
Read More

Peterson Number in C++

In this tutorial, we will learn about the Peterson number and write a C++ program to check if the given number is a Peterson number …
Read More