10X Sale
kh logo
All Courses
  1. Tutorials
  2. Programming Tutorials

Static Keyword in C#

Updated on Sep 3, 2025
 
46,000 Views

The static keyword is used to make a data item non-instantiable. It can be used with classes, methods, variables, constructors, operators etc. However, it cannot be used with destructors, indexers etc.

Some of the implementations of the static keyword are given as follows:

Static Class

A static class is non-instantiable i.e. a variable of the class cannot be created using the new keyword. So, the static class members have to be accessed using the class name itself.

A static class is defined using the keyword static. It can only have static data members and static methods. If this rule is not followed, there is a compile time error.

A program that demonstrates a static class is given as follows:

using System;
namespace StaticClassDemo
{
  static class SClass
  {
    public static int staticVar = 5;
    public static void staticMethod()
    {
        Console.WriteLine("Inside Static Method");
    }
   }
   class Test
   {
     static void Main(string[] args)
     {
        Console.WriteLine("Value of static variable: {0}", SClass.staticVar);
        SClass.staticMethod();   
     }
   }
}

Source Code: Program that demonstrates a static class in C#

The output of the above program is as follows:

Value of static variable: 5
Inside Static Method

Now let us understand the above program.

The static class SClass contains a static variable staticVar whose value is 5. Also the static method staticMethod() prints the statement "Inside Static Method". The code snippet for this is given as follows:

static class SClass
{
   public static int staticVar = 5;
   public static void staticMethod()
   {
       Console.WriteLine("Inside Static Method");
   }
}

Inside the main() function, the value of the static variable staticVar is displayed. It is accessed using the static class name i.e. SClass. Then the static method staticMethod() is called. The code snippet for this is given as follows:

static void Main(string[] args)
 {
    Console.WriteLine("Value of static variable: {0}", SClass.staticVar);
    SClass.staticMethod();
 }

Static Constructor

A static constructor initializes the static data members of a class. This is done implicitly when they are referenced for the first time i.e. when the class object is created. A static constructor does not contain any parameters.

A program that demonstrates a static constructor is given as follows:

using System;
namespace StaticConstructorDemo
{
  public class SClass
  {
    public int x;
    public int y;
    public static int z;
    public SClass()
    {
        x = 5;
        y = 8;
    }
    static SClass()
    {
        z = 10;
    }
    public void display()
    {
        Console.WriteLine("x = {0}", x);
        Console.WriteLine("y = {0}", y);
        Console.WriteLine("z = {0}", z);
    }
   }
   class Test
   {
     static void Main(string[] args)
     {
        SClass obj = new SClass();
        obj.display();
     }
   }
}

Source Code: Program that demonstrates a static constructor in C#

The output of the above program is as follows:

x = 5
y = 8
z = 10

Now let us understand the above program.

The class SClass has two variables x and y and one static variable z. A default constructor is used to initialize x and y but a static constructor is used to initialize z. The public method display() displays the values of x, y and z. The code snippet for this is given as follows:

 public class SClass
  {
    public int x;
    public int y;
    public static int z;
    public SClass()
    {
        x = 5;
        y = 8;
    }
    static SClass()
    {
        z = 10;
    }
    public void display()
    {
        Console.WriteLine("x = {0}", x);
        Console.WriteLine("y = {0}", y);
        Console.WriteLine("z = {0}", z);
    }
   }

The function main() contains an object obj of class SClass. Then the function display() is called which displays the values of x, y and z. The code snippet for this is given as follows:

static void Main(string[] args)
     {
        SClass obj = new SClass();
        obj.display();
     }
+91

By Signing up, you agree to ourTerms & Conditionsand ourPrivacy and Policy

Get your free handbook for CSM!!
Recommended Courses