Where is ImmutableArray?

一笑奈何 提交于 2019-12-10 14:26:14

问题


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 NullReferenceExceptions. 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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!