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