Difference between ImmutableArray<T> and ImmutableList<T>

强颜欢笑 提交于 2019-12-09 07:35:17

问题


What is difference between ImmutableArray<T> and ImmutableList<T>, and where would it be best to use each?


回答1:


I think you are asking where to use both. This blog will help otherwise some good points are mentioned here.

Use immutable array when:

  • Updating the data is rare or the number of elements is quite small (<16)
  • You need to be able to iterate over the data in performance critical sections
  • You have many instances of immutable collections and you can’t afford keeping the data in trees

Use immutable list when:

  • Updating the data is common or the number of elements isn't expected to be small
  • Updating the collection is more performance critical than iterating the contents



回答2:


Here is some reading that might help explain

http://blogs.msdn.com/b/dotnet/archive/2013/06/24/please-welcome-immutablearray.aspx

here's an excerpt

Reasons to use immutable array:

  • Updating the data is rare or the number of elements is quite small (<16)
  • you need to be able to iterate over the data in performance critical sections
  • you have many instances of immutable collections and you can’t afford keeping the data in trees

Reasons to stick with immutable list:

  • Updating the data is common or the number of elements isn’t expected to be small
  • Updating the collection is more performance critical than iterating the contents



回答3:


The main difference is that an ImmutableArray is just a wrapper around a normal array which means that retrieving elements within the array is extremely quick.

An ImmutableArray is also a struct meaning that it's a value type so it takes up less space than an Immutable List which is a reference type.

For these reasons an ImmutableArray is suitable to use if you rarely need to update its data or the number of elements is small (less than 16) or if the performance of iterating over the collection is important.

If your needs do not match the above reasons then you should use an ImmutableList.

Look here for the documentation.




回答4:


Going by the blog post "Please welcome ImmutableArray<T>" (the same blog post everyone else is referencing), the differences are...

ImmutableArray:

  • Simple implementation (just an array).
  • Not optimized for quickly updating large collections (the entire array needs to be copied).
  • More efficient for most use cases (anything that does not involve updating large collections).

ImmutableList:

  • More complicated implementation (something involving trees).
  • Is optimized for quickly updating large collections (it can be done in logarithmic time).
  • Less efficient for most use cases.

So if you're not updating much, or your collections are small, use ImmutableArray. If you're going to be frequently updating large collections, you'll need to use ImmutableList.



来源:https://stackoverflow.com/questions/22349861/difference-between-immutablearrayt-and-immutablelistt

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