What is a static class in C#?


The C# static class cannot be instantiated and can only have only static members. The static class in C# is sealed and cannot contain instance constructors.

The following is an example with static class and static members −

Example

using System;

public static class Demo {
   public static float PI = 3.14f;
   public static int calc(int n){return n*n;}
}

class Program {

   public static void Main(string[] args) {
      Console.WriteLine("PI: "+Demo.PI);
      Console.WriteLine("Square: " + Demo.calc(3));
   }
}

Above, the static class is −

public static class Demo {
   public static float PI = 3.14f;
   public static int calc(int n){return n*n;}
}

Above we have the following static members −

public static float PI = 3.14f;
public static int calc(int n){return n*n;}

Updated on: 21-Jun-2020

386 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements