Home > Backend Development > C#.Net Tutorial > What are static classes in C#?

What are static classes in C#?

WBOY
Release: 2023-09-03 09:49:02
forward
1367 people have browsed it

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));
   }
}
Copy after login

Above, the static class is-

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

Above we have the following static members -

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

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template