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.


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.


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.


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


C Program to search an element in an array using Pointers

A separate function( search_function()) will be created where the array pointer will be declared and the searched element along with the size of an array …

C Program to find the sum of the digits of a number using recursion function

This C program calculates the sum of digits of a given number using recursion. Here’s a concise explanation: Function Definition: sumDigits(int n) This function calculates …

C program to find factorial of a numberĀ using Ternary operator with Recursion

Recursion refers to the function calling itself directly or in a cycle. Before we begin, you should have the knowledge of following in C Programming: …

C Program to Add Two Numbers Using Call by Reference

The program takes the two numbers from the user and passes the reference to the function where the sum is calculated. You may go through …

Find the output ab, cd, ef, g for the input a,b,c,d,e,f,g in Javascript and Python

In this tutorial, we will write a program to find a pairs of elements from an array such that for the input [a,b,c,d,e,f,g] we will …

String Pattern Programs in C

In this tutorial, we will write various C pattern programs for String. Before that, you may go through the following topics in C. for loop …