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

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

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 20-Jun-2020

535 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements