What are generic collections in C#?

Generic collections in C# are type-safe collections that allow you to specify the type of elements at compile time. They provide better performance, type safety, and IntelliSense support compared to non-generic collections like ArrayList and Hashtable.

The most commonly used generic collections include List<T>, Dictionary<TKey, TValue>, SortedList<TKey, TValue>, Queue<T>, and Stack<T>. These are part of the System.Collections.Generic namespace.

Syntax

Following is the syntax for declaring generic collections −

List<T> listName = new List<T>();
Dictionary<TKey, TValue> dictName = new Dictionary<TKey, TValue>();
SortedList<TKey, TValue> sortedListName = new SortedList<TKey, TValue>();

Where T, TKey, and TValue are type parameters that specify the actual data types.

List<T>

List<T> is a generic collection that provides a dynamic array implementation. Unlike ArrayList, it is type-safe and offers better performance by avoiding boxing and unboxing operations.

List<T> vs ArrayList List<string> ? Type-safe ? No boxing/unboxing ? Better performance ArrayList ? No type safety ? Boxing/unboxing ? Runtime errors

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      // Initializing List<string> with collection initializer
      List<string> myList = new List<string>() {
         "one",
         "two",
         "three",
         "four",
         "five",
         "six"
      };
      
      Console.WriteLine("Count: " + myList.Count);
      Console.WriteLine("First element: " + myList[0]);
      
      // Adding and removing elements
      myList.Add("seven");
      myList.Remove("three");
      
      Console.WriteLine("After modifications:");
      foreach (string item in myList) {
         Console.WriteLine(item);
      }
   }
}

The output of the above code is −

Count: 6
First element: one
After modifications:
one
two
four
five
six
seven

SortedList<TKey, TValue>

SortedList<TKey, TValue> is a generic collection that maintains key-value pairs in sorted order by key. It combines the functionality of an array and a hash table, allowing access by both key and index.

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      SortedList<string, string> sl = new SortedList<string, string>();

      sl.Add("001", "Tim");
      sl.Add("002", "Steve");
      sl.Add("003", "Bill");
      sl.Add("004", "Tom");

      if (sl.ContainsValue("Bill")) {
         Console.WriteLine("This name is already in the list");
      } else {
         sl.Add("005", "James");
      }

      Console.WriteLine("Key-Value pairs:");
      foreach (var kvp in sl) {
         Console.WriteLine(kvp.Key + ": " + kvp.Value);
      }
      
      Console.WriteLine("\nAccessing by key '002': " + sl["002"]);
   }
}

The output of the above code is −

This name is already in the list
Key-Value pairs:
001: Tim
002: Steve
003: Bill
004: Tom

Accessing by key '002': Steve

Dictionary<TKey, TValue>

Dictionary<TKey, TValue> provides fast key-based lookups using hash tables. Unlike SortedList, it does not maintain sorted order but offers better performance for large datasets.

Example

using System;
using System.Collections.Generic;

class Program {
   static void Main() {
      Dictionary<int, string> students = new Dictionary<int, string>();
      
      students.Add(101, "Alice");
      students.Add(102, "Bob");
      students.Add(103, "Charlie");
      
      Console.WriteLine("Student 102: " + students[102]);
      
      // Safe key checking
      if (students.TryGetValue(104, out string name)) {
         Console.WriteLine("Found: " + name);
      } else {
         Console.WriteLine("Student 104 not found");
      }
      
      Console.WriteLine("\nAll students:");
      foreach (var student in students) {
         Console.WriteLine("ID: " + student.Key + ", Name: " + student.Value);
      }
   }
}

The output of the above code is −

Student 102: Bob
Student 104 not found

All students:
ID: 101, Name: Alice
ID: 102, Name: Bob
ID: 103, Name: Charlie

Comparison of Generic Collections

Collection Use Case Key Features
List<T> Dynamic arrays with indexed access Type-safe, resizable, supports LINQ
Dictionary<TKey, TValue> Fast key-based lookups Hash table, O(1) average access time
SortedList<TKey, TValue> Sorted key-value pairs Maintains sort order, slower insertions

Conclusion

Generic collections in C# provide type safety, better performance, and compile-time error checking compared to non-generic collections. List<T> is ideal for dynamic arrays, Dictionary<TKey, TValue> for fast lookups, and SortedList<TKey, TValue> when you need sorted key-value pairs.

Updated on: 2026-03-17T07:04:35+05:30

816 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements