Generic Collections in .NET - .NET (original) (raw)
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Generic collections in .NET
- Article
- 09/15/2021
In this article
The .NET class library provides a number of generic collection classes in the System.Collections.Generic and System.Collections.ObjectModel namespaces. For more detailed information about these classes, see Commonly Used Collection Types.
System.Collections.Generic
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 is a generic version of ArrayList. There are generic Queue and Stack 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> generic class is a pure dictionary and has no nongeneric counterpart.
The LinkedList generic class is a true linked list. It has no nongeneric counterpart.
System.Collections.ObjectModel
The Collection generic class provides a base class for deriving your own generic collection types. The ReadOnlyCollection class provides an easy way to produce a read-only collection from any type that implements the IList generic interface. The KeyedCollection<TKey,TItem> generic class provides a way to store objects that contain their own keys.
Other generic types
The Nullable generic structure allows you to use value types as if they could be assigned null
. This can be useful when working with database queries, where fields that contain value types can be missing. The generic type parameter can be any value type.
The ArraySegment generic structure provides a way to delimit a range of elements within a one-dimensional, zero-based array of any type. The generic type parameter is the type of the array's elements.
The EventHandler generic delegate eliminates the need to declare a delegate type to handle events, if your event follows the event-handling pattern used by .NET. For example, suppose you have created a MyEventArgs
class, derived from EventArgs, to hold the data for your event. You can then declare the event as follows:
public:
event EventHandler<MyEventArgs^>^ MyEvent;
public event EventHandler<MyEventArgs> MyEvent;
Public Event MyEvent As EventHandler(Of MyEventArgs)
See also
- System.Collections.Generic
- System.Collections.ObjectModel
- Generics
- Generic Delegates for Manipulating Arrays and Lists
- Generic Interfaces
Collaborate with us on GitHub
The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.