代码之家  ›  专栏  ›  技术社区  ›  aybe

为什么ImmutableList<T>是一个类,而ImmutableArray<T>是一个结构?

  •  1
  • aybe  · 技术社区  · 4 年前

    ImmutableArray ImmutableList

    • ImmutableArray<T> 已作为 struct
    • ImmutableList<T> class

    问题:

    你能解释一下为什么会做出这样的设计决定吗?

    尤其是,为什么 是一个 结构 .

    毕竟, System.Array 是一个 那么,为什么不是这样 ?

    2 回复  |  直到 4 年前
        1
  •  2
  •   paulsm4    4 年前

    本文对将.Net ImmutableArray设置为“结构”而不是类的决定提供了一些启示:

    https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/choosing-between-class-and-struct

    如果

    特点:

    • 它在逻辑上表示单个值,类似于基元类型(int、double等)。
    • 它是不变的。
    • 它不必经常装箱。

    在所有其他情况下,您应该将类型定义为类。

    另请参见: