Selecting a Collection Class
Microsoft Silverlight will reach end of support after October 2021. Learn more.
Be sure to choose your System.Collections class carefully. Using the wrong type can restrict your use of the collection.
Consider the following questions:
Do you need a sequential list where the element is typically discarded after its value is retrieved?
Do you need to access the elements in a certain order, such as FIFO, LIFO, or random?
The Queue<T> generic class offer FIFO access.
The Stack<T> generic class 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.
The rest of the collections offer random access.
Do you need to access each element by index?
The List<T> generic class offers access to zero elements by the zero-based index of the element.
The Dictionary<TKey, TValue> generic class offers access to its elements by the key of the element.
The KeyedCollection<TKey, TItem> generic class offers access to its 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 interface or the IList<T> generic interface.
One key and one value: Use any of the collections based on the IDictionary interface or the IDictionary<TKey, TValue> generic interface.
One value with embedded key: Use the KeyedCollection<TKey, TItem> generic class.
Do you need collections that accept only strings?
- You can use any of the generic collection classes in the System.Collections.Generic namespace as strongly typed string collections by specifying the String class for their generic type arguments.
LINQ to Objects
LINQ to Objects allows developers to use LINQ queries to access in-memory objects as long as the object type implements IEnumerable or IEnumerable<T>. LINQ queries provide a common pattern for accessing data, are typically more concise and readable than standard foreach loops and provide filtering, ordering and grouping capabilities. In addition, LINQ queries can also provide performance increases. For more information, see LINQ to Objects.
See Also