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.

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