问题
Why does ImmutableArray seem to not be there in Microsoft Immutable Collections NuGet package version 1.0.34?
回答1:
ImmutableArray
is not present in your version of the library.
As you can see in the version history, the release notes for 1.1.20 mention "Re-included ImmutableArray<T>"
You can find the explanation for why ImmutableArray was absent from the 1.0 version on the .NET blog, in this announcement. (In short - the Roslyn team had a noticeable performance hit when they tried to use that type instead of regular arrays, and the team in charge of the library was not sure how to fix that, while keeping a reasonable API.)
You will find newer versions of the library under its new NuGet package, System.Collections.Immutable.
N.B.: According to the source code in the new versions of System.Collections.Immutable
, they have apparently decided to take the API hit - that is, some operations on a unitialized ImmutableArray
will throw a surprising NullReferenceException
s. Clearly, ImmutableArray
should never be instantiated with new
. (ImmutableArray<T>.Empty
should be used instead)
回答2:
From the documentation:
public static class ImmutableArray
The ImmutableArray
is static, so you can't instantiate it. Use:
ImmutableArray.Create<T>(); // Creates an empty immutable array
来源:https://stackoverflow.com/questions/29273466/where-is-immutablearray