System.Collections.Generic

Overview

The System.Collections.Generic namespace contains interfaces and classes that define generic collections, which allow you to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.

Common Types

  • List<T> – Represents a strongly typed list of objects that can be accessed by index.
  • Dictionary<TKey,TValue> – Represents a collection of keys and values.
  • Stack<T> – Represents a last-in, first-out collection.
  • Queue<T> – Represents a first-in, first-out collection.
  • HashSet<T> – Represents a set of values.
  • LinkedList<T> – Represents a doubly linked list.

Example: Using List<T>

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List<int> numbers = new List<int> { 1, 2, 3, 4, 5 };
        numbers.Add(6);
        foreach (int n in numbers)
        {
            Console.WriteLine(n);
        }
    }
}
Imports System
Imports System.Collections.Generic

Module Program
    Sub Main()
        Dim numbers As New List(Of Integer) From {1, 2, 3, 4, 5}
        numbers.Add(6)
        For Each n As Integer In numbers
            Console.WriteLine(n)
        Next
    End Sub
End Module