2

When you read the documentation for ImmutableArray and ImmutableList, you can see that:

  • ImmutableArray<T> has been implemented as a struct
  • ImmutableList<T> has been implemented as a class

Question:

Can you explain why such design decision?

Especially, why ImmutableArray<T> is a struct.

After all, System.Array is a class so, why it isn't the case for ImmutableArray<T>?

aybe
  • 15,516
  • 9
  • 57
  • 105
  • 2
    From this post it sounds like the reason was for performance: https://devblogs.microsoft.com/dotnet/please-welcome-immutablearrayt/ – devNull Dec 16 '20 at 00:04

1 Answers1

2

This article sheds a bit of light on the decision to make .Net ImmutableArray a "struct" instead of a class:

Choosing Between Class and Struct

✔️ CONSIDER defining a struct instead of a class if instances of the type are small and commonly short-lived or are commonly embedded in other objects.

❌ AVOID defining a struct unless the type has all of the following characteristics:

  • It logically represents a single value, similar to primitive types (int, double, etc.).
  • It has an instance size under 16 bytes.
  • It is immutable.
  • It will not have to be boxed frequently.

In all other cases, you should define your types as classes.

See also:

KyleMit
  • 30,350
  • 66
  • 462
  • 664
paulsm4
  • 114,292
  • 17
  • 138
  • 190