Difference between ImmutableArray<T> and ImmutableList<T>
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
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
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 reasons above, you should use an ImmutableList
.
Look here for the documentation.