首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> 關於C語言

C# 常見問題:const 與 static readonly 差異深度解析

     編輯:關於C語言

核心差異:編譯期 vs 執行期

特性conststatic readonly
值確定時機編譯期 (必須為常數運算式)執行期 (靜態建構函式或宣告初始化)
允許型別基本型別、string、enum、null 參考任意型別 (含陣列、類別實例、結構)
記憶體不佔用儲存空間,值內嵌至呼叫端 IL儲存於型別靜態欄位,佔用記憶體
跨組件版本修改值需重編譯所有參考組件修改值僅需重編譯定義組件
可用位置欄位、區域變數僅限欄位 (不可用於區域變數/屬性)

可互換性測試


// 1. new 物件 -> 僅 static readonly
static readonly MyClass Obj1 = new MyClass(); // const 不可 (需執行建構函式)

// 2. null 參考 -> 兩者皆可
static readonly MyClass Obj2 = null;
const MyClass Obj3 = null; // 僅參考型別允許 null

// 3. 常數運算式 -> 兩者皆可
static readonly int A = B * 20;
static readonly int B = 10;
const int C = B * 20; // OK

// 4. 陣列/集合初始化 -> 僅 static readonly
static readonly int[] Primes = new int[] {2,3,5}; // const 不可

// 5. 區域變數 -> 僅 const
void Foo() { const int LocalConst = 10; /* static readonly 不可用於區域 */ }

經典應用場景:預定義物件實例

const 無法建構物件,static readonly 是標準做法:


public class Color
{
    public static readonly Color Black = new Color(0, 0, 0);
    public static readonly Color White = new Color(255, 255, 255);
    public static readonly Color Red   = new Color(255, 0, 0);
    public static readonly Color Green = new Color(0, 255, 0);
    public static readonly Color Blue  = new Color(0, 0, 255);

    private readonly byte _r, _g, _b;
    public Color(byte r, byte g, byte b) { _r=r; _g=g; _b=b; }
}

static readonly` 參考型別的可變性陷阱


public static readonly MyClass Instance = new MyClass();

// 合法:修改物件內部狀態
Instance.SomeProperty = 10;

// 非法:替換參考
Instance = new MyClass(); // 編譯錯誤:readonly 欄位不可指派

// 結構 (值型別) 不同:readonly 結構欄位連同內容皆不可變
public static readonly MyStruct S = new MyStruct();
// S.SomeField = 10; // 編譯錯誤:無法修改 readonly 結構的欄位

選擇指南

  1. 真正的編譯期常數 (數學常數、狀態碼、固定字串) → const。
  2. 需執行期建構、參考型別實例、陣列、或跨組件版本彈性 → static readonly。
  3. 區域常數 → const (唯一選項)。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved