若要定义整数类型(int、byte 等)的常量值,使用枚举类型。
若要定义非整型常量,一种方法是将它们分组到单个名为 Constants 的静态类中。 这要求对常量的所有引用都使用该类名作为前缀。
示例:
static class Constants
{
public const double Pi = 3.14159;
public const int SpeedOfLight = 300000; // km per sec.
}
class Program
{
static void Main()
{
double radius = 5.3;
double area = Constants.Pi * (radius * radius);
int secsFromSun = 149476000 / Constants.SpeedOfLight; // in km
}
}