Collection Classes in C#

What are Collection Classes in C#?

Collection classes in C# are predefined data structures provided by the .NET framework to store and manipulate objects efficiently. These collections are available in System.Collections (non-generic) and System.Collections.Generic (generic) namespaces.

Key Features of Collection Classes

  • Provide dynamic data structures that can grow or shrink as needed.
  • Offer built-in methods for adding, removing, sorting, and searching elements.
  • Available in both generic (List<T>, Dictionary<TKey, TValue>) and non-generic (ArrayList, Hashtable) forms.
  • Improve performance compared to traditional arrays by handling memory automatically.

Common Collection Classes in C#

Below are some commonly used collection classes along with their descriptions and use cases:

Collection Class Description Example Use Case
List<T> Dynamic array that can hold elements of a specific type. Managing a list of student names.
Dictionary<TKey, TValue> Key-value pair collection for fast lookups. Storing configuration settings.
Queue<T> First-In-First-Out (FIFO) collection. Processing a task queue in order.
Stack<T> Last-In-First-Out (LIFO) collection. Implementing undo/redo functionality.
HashSet<T> Stores unique elements only. Keeping track of unique student IDs.

Example: Using List<T>

The List<T> class provides a dynamically resizable array.

Example: Working with List<T>

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        List students = new List { "Alice", "Bob", "Charlie" };

        students.Add("David");
        students.Remove("Bob");

        Console.WriteLine("Student List:");
        foreach (var student in students)
        {
            Console.WriteLine(student);
        }
    }
}

// Output:
// Student List:
// Alice
// Charlie
// David
        

The List<T> class allows adding, removing, and accessing elements dynamically.

Example: Using Dictionary<TKey, TValue>

The Dictionary<TKey, TValue> class stores key-value pairs for fast lookups.

Example: Storing Key-Value Pairs in a Dictionary

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        Dictionary students = new Dictionary
        {
            {1, "Alice"},
            {2, "Bob"},
            {3, "Charlie"}
        };

        students[4] = "David"; // Adding a new key-value pair

        Console.WriteLine("Students:");
        foreach (var pair in students)
        {
            Console.WriteLine($"ID: {pair.Key}, Name: {pair.Value}");
        }
    }
}

// Output:
// Students:
// ID: 1, Name: Alice
// ID: 2, Name: Bob
// ID: 3, Name: Charlie
// ID: 4, Name: David
        

The Dictionary class allows efficient lookups using unique keys.

Best Practices for Using Collection Classes

  • Use List<T> when working with dynamic lists.
  • Use Dictionary<TKey, TValue> for fast key-based lookups.
  • Use Queue<T> for FIFO operations and Stack<T> for LIFO operations.
  • Use HashSet<T> when uniqueness is required.
  • Prefer generic collections over non-generic collections for type safety.