C# – Record, Class, Struct

前言

之前在 C# – 10.0 已經有介紹過 Record 了. 但之前還沒怎麼用到, 最近有用到了, 所以特別寫多一篇. 

 

Class vs Struct

參考: C#詳解struct和class的區別

它們最大的區別在於 Class 是引用類型, Struct 是值類型. 引用類型 (heap) vs 值類型 (stack)

定義是差不多的, 可以有 property, method, constructor 等等

public struct DimensionStruct
{
    public int Width { get; set; }
    public int Height { get; set; }
}

public class DimensionClass
{
    public int Width { get; set; }
    public int Height { get; set; }
}

在對比值和引用的時候就看區別了

var dimensionClass = new DimensionClass { Width = 100, Height = 100 };
var dimensionClass2 = dimensionClass;
var d = Object.ReferenceEquals(dimensionClass, dimensionClass2); // true
var e = Object.Equals(dimensionClass, dimensionClass2); // true
var f = Object.Equals(dimensionClass, new DimensionClass { Width = 100, Height = 100 }); // false 雖然裏面 value 一樣

實例指向同一個地址, Object.Equals 對比的是它們的地址是否一致, 而不是值是否一致.

在看 Struct

var dimensionStruct = new DimensionStruct { Width = 100, Height = 100 };
var dimensionStruct2 = dimensionStruct;
var a = Object.ReferenceEquals(dimensionStruct, dimensionStruct2); // false
var b = Object.Equals(dimensionStruct, dimensionStruct2); // true
var c = Object.Equals(dimensionStruct, new DimensionStruct { Width = 100, Height = 100 }); // true 值一樣就行了

地址肯定是不一樣的了. Object.Equals 對比的是裏面的值. 值一樣就行了.

When to use Struct?

我個人是沒有特別感覺什麼情況非用 Struct 不可.

比較常見的是 Size (width, height), Coordinate (x, y) 這類的 object value 就會用 struct.

 

Record

參考:

Intro to Records in C# 9 - How To Use Records And When To Use Them

Record Structs

Record Structs

Record 是 9.0 出來的. 先看看它的特色.

Definition

首先是它的 Definition 可以很短

public record Dimension(int Width, int Height);

3 大特色

record 有 3 大特色

1. immutable

函數式提倡的東西. 對象創建後, 屬性值就不可以修改了.

var dimension = new Dimension(Width: 100, Height: 200);
dimension.Width = 5; // Error: record 是 immutable

上面這樣是直接 complie error 的.

2. Deconstruct

類似 JavaScript 的解構, 它利用 Tuple 解構的特性.

var dimension = new Dimension(Width: 100, Height: 200);
var (width, height) = dimension; // base on sequence

// int width;
// (width, var height) = dimension; // 賦值給 existing variable  


// var (_, height) = dimension; // underscore for skip

對比 JS 還是輸一點, 沒有那麼靈活.

3. assign and clone

immutable 要怎樣修改 value 呢? 靠的是 clone 一個新的來賦值.

var dimension = new Dimension(Width: 100, Height: 200);
var newDimension = dimension with { Height = 100 };

比起 JS 還是輸很多. 但至少是跨出去了.

Record vs Class

我們先講雷同的地方. 上面 3 大特色, Class 可以完成 immutable 和 Deconstruct

public class DimensionClass
{
    public DimensionClass(int width, int height)
    {
        Width = width;
        Height = height;
    }

    public int Width { get; init; } // 在 setter 使用關鍵字 init 就可以實現 immutable 了
    public int Height { get; init; }

    internal void Deconstruct(out int width, out int height)
    {
        width = Width;
        height = Height;
    }

    // 這樣寫也可以
    // internal void Deconstruct(out int width, out int height) => (width, height) = (Width, Height);
}

使用

var dimensionClass = new DimensionClass(width: 100, height: 200);
// dimensionClass.Width = 5; // error
var (width, height) = dimensionClass;

關鍵就在 setter 的 init 和 Deconstruct 方法.

主要的區別是 Class 的 Definition 比起 Record 太繁瑣了.

但是, 如果 Record 你想要添加 Constructor 的話, 它就會變得和 Class 一模一樣繁瑣了.

public record Dimension
{
    public Dimension(int width, int height)
    {
        Width = width;
        Height = height;
    }
    public int Width { get; init; }
    public int Height { get; set; } // 定義 setter 的話, Record 就不是 immutable 了哦

    // 因爲不是用默認的 constructor 所以需要另外定義 Deconstruct
    internal void Deconstruct(out int width, out int height) => (width, height) = (Width, Height);
}

setter 用 set 的話可以讓 record 變成不是 immutable, 當有了 constructor 就需要另外定義 Deconstruct 方法了.

所以 Record 和 Class 的區別在 immutable 和 Deconstruct 其實差不多.

只是 Record 有一些 default 邏輯, 在簡單的案例中會比 Class 更方便定義.

Record 比 Class 厲害的是第三特點 assign and clone. 關鍵字 with 只能用在 record. Class 無法做到.

但是 ! 在 C# 10.0 之後, 多了一個 mix. 叫 record class.

 

加上 record 關鍵字後, 這個 Class 就可以使用 with 關鍵字了. 至此 Class 和 Record 更像了.

Object.Equals(record)

Class 和 Record 都是引用類似. 但是 Object.Equals 對 Class, 是比較 reference, 而對 record 和 record class 是比較值.

When to use?

如果你有喜歡 immutable 和 with 這類特性, 那麼就可以考慮用 record.

至於是 record 還是 record class. 夠簡單就用 record. 複雜一點的話就儘量用 record class.

 

Record class overload constructor error

遇到一個 error, 不清楚什麼原因, 先記入在這裏.

A copy constructor in a record must call a copy constructor of the base, or a parameterless object constructor if the record inherits from object. [TestRImage]csharp(CS8868)

引發的原因是 record class + overload constructor + parameter is class object + calling this 

目前沒空檢查. 解決方法就是不要 call this.

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章