c#怎么调用静态常量

2025-02-23 01:54:40
推荐回答(1个)
回答1:

首先定义静态常量。C#中,静态常量必须定义在类中

public class MyGlobalConstants
{
   public static readonly string Name = "ABC";
   public static readonly int Count = 10000;
   public static readonly float PI = 3.14f; 
}

然后,在程序任意地方可以使用这些静态常量。使用方式为:类名.静态常量名

string name = MyGlobalConstants.Name + "123"; // name="ABC123"
float area = (10*10)* MyGlobalConstants.PI;
……