常量(C# 编程指南)Constants (C# Programming Guide)

常量是不可变的值,在编译时是已知的,在程序的生命周期内不会改变。Constants are immutable values which are known at compile time and do not change for the life of the program. 常量使用 const 修饰符声明。Constants are declared with the const modifier. 仅 C# 内置类型(不包括 System.Object)可声明为 constOnly the C# built-in types (excluding System.Object) may be declared as const. 有关内置类型的列表,请参阅内置类型表For a list of the built-in types, see Built-In Types Table. 用户定义的类型(包括类、结构和数组)不能为 constUser-defined types, including classes, structs, and arrays, cannot be const. 使用 readonly 修饰符创建在运行时一次性(例如在构造函数中)初始化的类、结构或数组,此后不能更改。Use the readonly modifier to create a class, struct, or array that is initialized one time at runtime (for example in a constructor) and thereafter cannot be changed.

C# 不支持 const 方法、属性或事件。C# does not support const methods, properties, or events.

枚举类型使你能够为整数内置类型定义命名常量(例如 intuintlong 等)。The enum type enables you to define named constants for integral built-in types (for example int, uint, long, and so on). 有关详细信息,请参阅枚举For more information, see enum.

常量在声明时必须初始化。Constants must be initialized as they are declared. 例如:For example:

class Calendar1
{
    public const int Months = 12;
}

在此示例中,常量 months 始终为 12,即使类本身也无法更改它。In this example, the constant months is always 12, and it cannot be changed even by the class itself. 实际上,当编译器遇到 C# 源代码中的常量标识符(例如,months)时,它直接将文本值替换到它生成的中间语言 (IL) 代码中。In fact, when the compiler encounters a constant identifier in C# source code (for example, months), it substitutes the literal value directly into the intermediate language (IL) code that it produces. 因为运行时没有与常量相关联的变量地址,所以 const 字段不能通过引用传递,并且不能在表达式中显示为左值。Because there is no variable address associated with a constant at run time, const fields cannot be passed by reference and cannot appear as an l-value in an expression.

备注

引用其他代码(如 DLL)中定义的常量值时要格外小心。Use caution when you refer to constant values defined in other code such as DLLs. 如果新版本的 DLL 定义了新的常量值,则程序仍将保留旧的文本值,直到根据新版本重新编译。If a new version of the DLL defines a new value for the constant, your program will still hold the old literal value until it is recompiled against the new version.

可以同时声明多个同一类型的常量,例如:Multiple constants of the same type can be declared at the same time, for example:

class Calendar2
{
    public const int Months = 12, Weeks = 52, Days = 365;
}

如果不创建循环引用,则用于初始化常量的表达式可以引用另一个常量。The expression that is used to initialize a constant can refer to another constant if it does not create a circular reference. 例如:For example:

class Calendar3
{
    public const int Months = 12;
    public const int Weeks = 52;
    public const int Days = 365;

    public const double DaysPerWeek = (double) Days / (double) Weeks;
    public const double DaysPerMonth = (double) Days / (double) Months;
}

可以将常量标记为publicprivateprotectedinternalprotected internalprivate protectedConstants can be marked as public, private, protected, internal, protected internal or private protected. 这些访问修饰符定义该类的用户访问该常量的方式。These access modifiers define how users of the class can access the constant. 有关详细信息,请参阅访问修饰符For more information, see Access Modifiers.

常量是作为静态字段访问的,因为常量的值对于该类型的所有实例都是相同的。Constants are accessed as if they were static fields because the value of the constant is the same for all instances of the type. 不使用 static 关键字来声明这些常量。You do not use the static keyword to declare them. 不在定义常量的类中的表达式必须使用类名、句点和常量名称来访问该常量。Expressions that are not in the class that defines the constant must use the class name, a period, and the name of the constant to access the constant. 例如:For example:

int birthstones = Calendar.Months;

C# 语言规范C# Language Specification

有关详细信息,请参阅 C# 语言规范For more information, see the C# Language Specification. 该语言规范是 C# 语法和用法的权威资料。The language specification is the definitive source for C# syntax and usage.

请参阅See also