Skip to content
BigBro222's Blog
LinkedInGitHub

Collections in C# (The basics 2)

C# basics3 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.

generic_non_mapping.png
Figure 1 A mapping between generic and nongeneric collections

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.

collections.png
Figure 2 System.Collections

4. How to choose a collection ?

Consider the following questions:

Collections in C# (The basics 1)

Collections in C# (The basics 2)

Collections in C# (The basics 3)

Reference

Selecting a Collection class from Microsoft

© 2023 by BigBro222's Blog. All rights reserved.