What is a static class in C#?

CsharpProgrammingServer Side Programming

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;}
raja
Published on 13-Aug-2018 15:55:23
Advertisements