Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between ImmutableArray<T> and ImmutableList<T>

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

like image 750
user3391731 Avatar asked Mar 12 '14 11:03

user3391731


People also ask

When to use Immutable array c#?

Immutable objects are useful when thread safety is a concern and/or when an instance of an object must be accessed outside of your code in a readonly mode. Advantages: Thread safety. It is secure to pass a reference of an immutable object outside of a class without the risk that the data could be changed.

Is array immutable in C#?

An array is not immutable, even if the elements it holds are immutable. So if an array slot holds a string, you can change it to a different string. This does not change any of the strings, it just changes the array.


2 Answers

Here is some reading that might help explain: Please welcome ImmutableArray

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
like image 123
Adween Avatar answered Sep 28 '22 00:09

Adween


I think you are asking where to use each of them. Please welcome ImmutableArray will help. To summarize, 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
like image 27
Rajeev Ranjan Avatar answered Sep 28 '22 02:09

Rajeev Ranjan