C#idiomatic C#
collectionsstandard/generics

Core library

Generic collections in .NET

Learn more about: Generic collections in .NET

  • page standard/generics/collections
  • updated 02/15/2018

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. System.Collections.Generic.Dictionary is a generic version of System.Collections.Hashtable; it uses the generic structure System.Collections.Generic.KeyValuePair for enumeration instead of System.Collections.DictionaryEntry.

System.Collections.Generic.List is a generic version of System.Collections.ArrayList. There are generic System.Collections.Generic.Queue and System.Collections.Generic.Stack classes that correspond to the nongeneric versions.

There are generic and nongeneric versions of System.Collections.Generic.SortedList. Both versions are hybrids of a dictionary and a list. The System.Collections.Generic.SortedDictionary generic class is a pure dictionary and has no nongeneric counterpart.

The System.Collections.Generic.LinkedList generic class is a true linked list. It has no nongeneric counterpart.

System.Collections.ObjectModel

The System.Collections.ObjectModel.Collection generic class provides a base class for deriving your own generic collection types. The System.Collections.ObjectModel.ReadOnlyCollection class provides an easy way to produce a read-only collection from any type that implements the System.Collections.Generic.IList generic interface. The System.Collections.ObjectModel.KeyedCollection generic class provides a way to store objects that contain their own keys.

Other generic types

The System.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 System.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 System.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 System.EventArgs, to hold the data for your event. You can then declare the event as follows:

sample · csharpsamples/snippets/csharp/VS_Snippets_CLR/conceptual.generics.overview/cs/source2.cs7

csharp
//<snippet6>
using System;

public class MyEventArgs : EventArgs
{
    private string msg;

    public MyEventArgs(string messageArg)
    {
        msg = messageArg;
    }

    public string Message
    {
        get {return msg;}
    }
}

class Dummy
{
    //<snippet7>
    public event EventHandler<MyEventArgs> MyEvent;
    //</snippet7>

    public static void Main()
    {
        Dummy dummo = new Dummy();

        dummo.DoEvent();
    }

    public void DoEvent()
    {
        MyEventArgs eArgs = new MyEventArgs("My Event Message");

        // MyEvent += new EventHandler<MyEventArgs>(MyEventHandler);
        //    or ...
        MyEvent += MyEventHandler;
        MyEvent(this, eArgs);
    }

    private void MyEventHandler(object sender, MyEventArgs eArgs)
    {
        Console.WriteLine(eArgs.Message);
    }
}
//</snippet6>

sample · vbsamples/snippets/visualbasic/VS_Snippets_CLR/conceptual.generics.overview/vb/source2.vb7

vb
'<snippet6>
Public Class MyEventArgs
    Inherits EventArgs
    Private msg As String

    Public Sub New(messageArg As String)
        msg = messageArg
    End Sub

    Public ReadOnly Property Message As String
        Get
            Return msg
        End Get
    End Property
End Class

Class Dummy
    '<snippet7>
    Public Event MyEvent As EventHandler(Of MyEventArgs)
    '</snippet7>

    Public Shared Sub Main()
        Dim dummo As New Dummy()

        dummo.DoEvent()
    End Sub

    Public Sub DoEvent()
        Dim eArgs As New MyEventArgs("My Event Message")

        ' AddHandler MyEvent, CType(AddressOf MyEventHandler, EventHandler(Of MyEventArgs))
        '    or ...
        AddHandler MyEvent, AddressOf MyEventHandler
        RaiseEvent MyEvent(Me, eArgs)
    End Sub

    Private Sub MyEventHandler(sender As Object, eArgs As MyEventArgs)
        Console.WriteLine(eArgs.Message)
    End Sub
End Class
'</snippet6>

See also