原文:https://blogs.msdn.microsoft.com/mazhou/2017/11/21/c-7-series-part-6-read-only-structs/
背景
在.NET世界中,有兩種基本型別:參考型別和值型別,簡單地說,參考型別是可以繼承/擴展的類,當傳遞參考型別物件時,傳遞的是一個“指標”;值型別是不能繼承/擴展的結構,當傳遞值型別物件時,傳遞的是一個“副本”,
C#中的struct是一個值型別,它“內部繼承”自System.ValueType,(我說的是結構之間沒有繼承,)
當在引數中使用struct時,會生成struct的副本,使用struct可能是高效的,因為它減少了堆物件分配的時間,
在許多場景中,開發人員使用結構作為傳遞值的有效方法,例如方法的回傳物件,或者跨應用程式使用的基本資料結構,
只讀結構
只讀結構是其公共成員為只讀的結構,就好像“this”變數一樣,
看一下下面的宣告:
public struct S { public int Age { get; set; } public string Name { get; set; } public S(int age, string name) { this.Age = age; this.Name = name; } public S(S other) { this = other; } public S Replace(S other) { S value = this; this = other; return value; } }
可以看到,我可以完全訪問已宣告的屬性Age和Name,還可以訪問this變數,這樣就可以用另一個實體S來替換this實體,
如果我在宣告中添加readonly修飾符,我的訪問權限將受到限制:
- 所有的成員(屬性、欄位)必須是自讀;
- 我需要在公共的有參建構式中初始化成員;
- 除了在建構式中,“this”變數在其他地方都是只讀的;
- 你不能定義“類欄位”事件;
下面的截圖顯示了上面的代碼改成了readonly struct后需要修正的地方,

下面是修改后的代碼:
public readonly struct S { public int Age { get; } public string Name { get; } public S(int age, string name) { this.Age = age; this.Name = name; } public S(S other) { this = other; } }
你可以像往常一樣初始化S的新實體,但是你不能修改任何實體的任何成員,你應該總是呼叫有參(而不是無參)建構式來正確初始化實體物件,否則您將獲得實體的默認值(所有成員都被初始化為成員型別的默認值),
private static void Test() { S s = new S(18, "Anna"); ref S other = ref s; other = new S(other); bool equal = s.Equals(other); // true. }
結論
只讀結構是一個方便的特性,可以幫助保護你的值被意外修改的影響;與其他新特性相結合(例如,ref結構和in引數),它將使你的C#代碼更容易地面向低級別的編程,在接下來的幾篇文章中,我將解釋所有這些新事物,請注意,你需要C# 7.2才能使用這個特性,它在Visual Studio 2017.5 Preview 4或更高版本中可用,
系列文章:
- [譯]C# 7系列,Part 1: Value Tuples 值元組
- [譯]C# 7系列,Part 2: Async Main 異步Main方法
- [譯]C# 7系列,Part 3: Default Literals 默認文本運算式
- [譯]C# 7系列,Part 4: Discards 棄元
- [譯]C# 7系列,Part 5: private protected 訪問修飾符
- [譯]C# 7系列,Part 6: Read-only structs 只讀結構 (本文)
- [譯]C# 7系列,Part 7: ref Returns ref回傳結果
- [譯]C# 7系列,Part 8: in Parameters in引數
- [譯]C# 7系列,Part 9: ref structs ref結構
- [譯]C# 7系列,Part 10: Span<T> and universal memory management Span<T>和統一記憶體管理 (完)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/91519.html
標籤:C#
