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

 Live Demo

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

Output

PI: 3.14
Square: 9

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 08-Aug-2018 13:01:04
Advertisements