const(C# 参考)const (C# Reference)

使用 const 关键字来声明某个常量字段或常量局部变量。You use the const keyword to declare a constant field or a constant local. 常量字段和常量局部变量不是变量并且不能修改。Constant fields and locals aren't variables and may not be modified. 常量可以为数字、布尔值、字符串或 null 引用。Constants can be numbers, Boolean values, strings, or a null reference. 不要创建常量来表示你需要随时更改的信息。Don’t create a constant to represent information that you expect to change at any time. 例如,不要使用常量字段来存储服务的价格、产品版本号或公司的品牌名称。For example, don’t use a constant field to store the price of a service, a product version number, or the brand name of a company. 这些值会随着时间发生变化;因为编译器会传播常量,所以必须重新编译通过库编译的其他代码以查看更改。These values can change over time, and because compilers propagate constants, other code compiled with your libraries will have to be recompiled to see the changes. 另请参阅 readonly 关键字。See also the readonly keyword. 例如:For example:

const int X = 0;
public const double GravitationalConstant = 6.673e-11;
private const string ProductName = "Visual C#";

备注Remarks

常数声明的类型指定声明引入的成员类型。The type of a constant declaration specifies the type of the members that the declaration introduces. 常量局部变量或常量字段的初始值设定项必须是一个可以隐式转换为目标类型的常量表达式。The initializer of a constant local or a constant field must be a constant expression that can be implicitly converted to the target type.

常数表达式是在编译时可被完全计算的表达式。A constant expression is an expression that can be fully evaluated at compile time. 因此,对于引用类型的常数,可能的值只能是 string 和 null 引用。Therefore, the only possible values for constants of reference types are string and a null reference.

常数声明可以声明多个常数,例如:The constant declaration can declare multiple constants, such as:

public const double X = 1.0, Y = 2.0, Z = 3.0;

不允许在常数声明中使用 static 修饰符。The static modifier is not allowed in a constant declaration.

常数可以参与常数表达式,如下所示:A constant can participate in a constant expression, as follows:

public const int C1 = 5;
public const int C2 = C1 + 100;

备注

readonly 关键字与 const 关键字不同。The readonly keyword differs from the const keyword. const 字段只能在该字段的声明中初始化。A const field can only be initialized at the declaration of the field. readonly 字段可以在声明或构造函数中初始化。A readonly field can be initialized either at the declaration or in a constructor. 因此,根据所使用的构造函数,readonly 字段可能具有不同的值。Therefore, readonly fields can have different values depending on the constructor used. 另外,虽然 const 字段是编译时常量,但 readonly 字段可用于运行时常量,如此行所示:public static readonly uint l1 = (uint)DateTime.Now.Ticks;Also, although a const field is a compile-time constant, the readonly field can be used for run-time constants, as in this line: public static readonly uint l1 = (uint)DateTime.Now.Ticks;

示例Example

public class ConstTest 
{
    class SampleClass
    {
        public int x;
        public int y;
        public const int C1 = 5;
        public const int C2 = C1 + 5;

        public SampleClass(int p1, int p2) 
        {
            x = p1; 
            y = p2;
        }
    }

    static void Main()
    {
        var mC = new SampleClass(11, 22);
        Console.WriteLine($"x = {mC.x}, y = {mC.y}");
        Console.WriteLine($"C1 = {SampleClass.C1}, C2 = {SampleClass.C2}");
    }
}
/* Output
    x = 11, y = 22
    C1 = 5, C2 = 10
*/

示例Example

此示例说明如何将常数用作局部变量。This example demonstrates how to use constants as local variables.

public class SealedTest
{
    static void Main()
    {
        const int C = 707;
        Console.WriteLine($"My local constant = {C}");
    }
}
// Output: My local constant = 707

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