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

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 …
Read More

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 …
Read More

Java Program to Find pair of Integers in Array whose sum is given Number

In this tutorial, we will write a program to find a pair of elements from an array whose sum equals a given number in java …
Read More

Program to Print Diamond Alphabet Patterns in C

In this tutorial, we will learn to write a C program to print Diamond patterns using alphabets/characters. However, in this tutorial, we will create a …
Read More

Half Diamond Pattern in C using Alphabets

In this tutorial, we will learn and code the half diamond alphabet patterns in C programming language. However, in this tutorial, we will create a …
Read More

Half Pyramid of Alphabets in C

In this tutorial, we will learn and code alphabet patterns in C programming language specifically the Half pyramid of alphabets in C programming. However, in …
Read More