Concurrent and immutable Collections in C#-Primer

Concurrent(or Threadsafe) collections allow multiple threads to update them simultaneously in a safe way. Concurrent collections are more efficient than implementing a regular collection with a lock.
An immutable collection cannot actually be modified. Instead, to modify an immutable collection, you create a new collection that represents the modified collection.

The .Net framework has several different collection types.
ImmutableList<T> Use this data structure when
a. the collection data does not change very often
b. the collection can be accessed by multiple threads safely.
c. They allow indexing like Lists but they have lower performance than Lists.

ImmutableStack<T> Use this data structure when
a. you need a first-in, first-out data structure
b. the collection data does not change very often
b. the collection can be accessed by multiple threads safely.
c. ImmutableStack have lower performance than Stack data structures.

ImmutableQueue<T> Use this data structure when
a. you need a first-in, last-out data structure
b. the collection data does not change very often
b. the collection can be accessed by multiple threads safely.
c. ImmutableQueue have lower performance than Queue data structures.

ImmutableDictionary<T, TValue> is a key-value collection of items. Use this type when you
a. need a key-value collection of items.
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items in ImmutableDictionary have an unpredictable order.
Performance of ImmutableDictionary is faster than ImmutableSortedDictionary.

ImmutableSortedDictionary<T, TValue> is a key-value collection of items sorted by key. Use this type when you
a. need a key-value collection of items.
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items are sorted in the collection.

ImmutableHashSet<T> is just a collection of unique items. Use this type when you
a. do not need to store duplicates
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items in the ImmutableHashSet have an unpredictable order.
Performance of ImmutableHashSet is faster than ImmutableSortedHashSet.

ImmutableSortedSet<T> is a sorted collection of unique items. Use this type when you
a. do not need to store duplicates
b. the collection data does not change very often
c. can be accessed by multiple threads safely.
d. the items are sorted in the collection.

ConcurrentDictionary<T, TValue> is a key-value collection of items. Use this type when you
a. need a key-value collection of items.
b. can be accessed by multiple threads safely.
Use the ConcurrentDictionary<T, TValue> when you have multiple threads reading and writing to the shared collection. If the updates are more rare then the ImmutableDictionary<T, TValue> is a better choice.

You may also like

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.