Collections in C# (The basics 2)
— C# basics — 3 min read
3. System.Collection
The System.Collection.Generic namespace came out during .Net 2.0, so the System.Collection is a bit obselete (cause it’s not type-safe and not efficient with box and unbox)
The classes in the System.Collections namespace do not store elements as specifically typed objects, but as objects of type Object. Whenever possible, you should use the generic collections in the System.Collections.Generic namespace or the System.Collections.Concurrent namespace instead of the legacy types in the System.Collections namespace.
Many of the generic collection types are direct analogs of nongeneric types. Dictionary<TKey,TValue> is a generic version of Hashtable; it uses the generic structure KeyValuePair<TKey,TValue> for enumeration instead of DictionaryEntry. List<T> is a generic version of ArrayList. There are generic Queue<T> and Stack<T>classes that correspond to the nongeneric versions. There are generic and nongeneric versions of SortedList<TKey,TValue> . Both versions are hybrids of a dictionary and a list. The SortedDictionary<TKey,TValue> a generic class is a pure dictionary and has no nongeneric counterpart. The LinkedList<T> a generic class is a true linked list and has no nongeneric counterpart.
4. How to choose a collection ?
Consider the following questions:
- A sequential list where the element is discarded after its value is retrieved?
- If yes, Queue, Queue<T> , Stack , Stack<T> , ConcurrentQueue<T> , ConcurrentStack<T>. ImmutableQueue<T> and ImmutableStack<T>.
- If not, consider using the other collections.
- Access the elements in a certain order?
- Queue, Queue<T>, ConcurrentQueue<T>, ImmutableQueue<T> offer FIFO access.
- Stack , Stack<T>,ConcurrentStack<T>, ImmutableStack<T>,offer LIFO access.
- The LinkedList<T> generic class allows sequential access either from the head to the tail, or from the tail to the head.
- Access each element by index?
- ArrayList, StringCollection, List<T>, ImmutableArray<T>, and ImmutableList<T> offer access to their elements by the zero-based index of the element.
- The Hashtable, SortedList, ListDictionary, and StringDictionary, Dictionary<TKey,TValue>, SortedDictionary<TKey,TValue> , ImmutableHashSet<T>, ImmutableDictionary<TKey,TValue>, ImmutableSortedSet<T>, and ImmutableSortedDictionary<TKey,TValue>. offer access to their elements by key .
- The NameObjectCollectionBase and NameValueCollection classes, and the KeyedCollection<TKey,TItem> and SortedList<TKey,TValue> generic classes offer access to their elements by either the zero-based index or the key of the element.
- Will each element contain one value, a combination of one key and one value, or a combination of one key and multiple values?
- One value: Use any of the collections based on the IList , IList<T> or IImmutableList<T> interface.
- One key and one value: Use any of the collections based on the IDictionary , IDictionary<TKey,TValue>, IImmutableSet<T> or IImmutableDictionary<TKey,TValue> interfaces.
- One value with embedded key: Use the KeyedCollection<TKey,TItem> generic class.
- One key and multiple values: Use the NameValueCollection class.
- Sort the elements differently from how they were entered?
- The Hashtable class sorts its elements by their hash codes.
- The SortedList class, and the SortedList<TKey,TValue> and SortedDictionary<TKey,TValue> generic classes sort their elements by the key. The sort order is based on the implementation of the IComparer interface for the SortedList class and on the implementation of the IComparer<T> generic interface for the SortedList<TKey,TValue> and SortedDictionary<TKey,TValue> generic classes. Of the two generic types, SortedDictionary<TKey,TValue> offers better performance than SortedList<TKey,TValue>, while SortedList<TKey,TValue> consumes less memory.
- ArrayList provides a Sort method that takes an IComparer implementation as a parameter. Its generic counterpart, the List<T> generic class, provides a Sort method that takes an implementation of the IComparer<T> generic interface as a parameter.
- Do you need fast searches and retrieval of information?
- ListDictionary is faster than Hashtable for small collections (10 items or fewer). The Dictionary<TKey,TValue> generic class provides faster lookup than the SortedDictionary<TKey,TValue> generic class. The multi-threaded implementation is ConcurrentDictionary<TKey,TValue>. ConcurrentBag<T> provides fast multi-threaded insertion for unordered data. For more information about both multi-threaded types, see When to Use a Thread-Safe Collection.
- Do you need collections that accept only strings?
- StringCollection, StringDictionary ( (System.Collections.Specialized ).
- List<String> or Dictionary<String, String>(System.Collections.Generic)
Collections in C# (The basics 1)
Collections in C# (The basics 2)
Collections in C# (The basics 3)