top

Search

C# Tutorial

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 ClassA 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:Source Code: Program that demonstrates a static class in C#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();         }    } }The output of the above program is as follows:Value of static variable: 5 Inside Static MethodNow 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 ConstructorA 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:Source Code: Program that demonstrates a static constructor in C#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();      }    } }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();      }
logo

C# Tutorial

Static Keyword in C#

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:

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

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();   
     }
   }
}

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:

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

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();
     }
   }
}

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();
     }

Leave a Reply

Your email address will not be published. Required fields are marked *

Suggested Tutorials

Swift Tutorial

Introduction to Swift Tutorial
Swift Tutorial

Introduction to Swift Tutorial

Read More

R Programming Tutorial

R Programming

Python Tutorial

Python Tutorial