Home  >  Article  >  Backend Development  >  What are static classes in C#?

What are static classes in C#?

WBOY
WBOYforward
2023-09-03 09:49:021303browse

C# 中的静态类是什么?

C# Static classes cannot be instantiated and can only have static members. Static classes in C# are sealed and cannot contain instance constructors.

The following is an example containing 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;}

The above is the detailed content of What are static classes in C#?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete