What is difference between ImmutableArray<T>
and ImmutableList<T>
, and where would it be best to use each?
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With