Collections General Overview

ยท

3 min read

Things become better to work when everything is organized. Same applicable while coding, we want to keep data organized. We can achieve this using Collections

C# provides a rich set of collection classes to store and manage groups of objects efficiently. ๐Ÿ“ฆ These collections are categorized into two primary groups Generic Collections and Non-Generic Collections.

1. Non-Generic Collections: These collections do not have a specific type parameter. They are less type-safe compared to generic collections.

  • ArrayList ๐Ÿ“œ

    • Dynamically resizable array of objects.

    • Can store elements of any data type.

    • Less type-safe, as you need to cast elements when retrieving them.

  • Hashtable ๐Ÿ—๏ธ

    • Stores key-value pairs.

    • Keys must be unique.

    • Not strongly typed, requiring manual casting.

  • Stack ๐Ÿฅž

    • Follows Last-In-First-Out (LIFO) order.

    • Useful for operations like undo/redo.

  • Queue ๐ŸšŒ

    • Follows First-In-First-Out (FIFO) order.

    • Ideal for processing items in the order they were added.

2. Generic Collections: These collections are type-safe, meaning you specify the data type of the elements they can hold. They are generally preferred over non-generic collections due to their type safety and performance benefits.

  • List<T> ๐Ÿ“‹

    • Ordered collection of elements.

    • Supports indexing, adding, removing, and searching elements.

  • Dictionary<TKey, TValue> ๐Ÿ“š

    • Stores key-value pairs.

    • Keys must be unique.

    • Provides efficient lookup based on keys.

  • HashSet<T> ๐ŸŽฒ

    • Unordered collection of unique elements.

    • Optimized for fast lookup and removal.

  • SortedSet<T> ๐Ÿ“ˆ

    • Ordered collection of unique elements.

    • Elements are sorted automatically.

  • Stack<T> ๐Ÿฅž

    • Generic version of the Stack class.
  • Queue<T> ๐ŸšŒ

    • Generic version of the Queue class.

Choosing the Right Collection ๐ŸŽฏ

The choice of collection depends on your specific requirements:

  • Ordered vs. Unordered: If you need to access elements by index or maintain a specific order, use List<T> or SortedSet<T>. For unordered collections, consider HashSet<T>.

  • Unique vs. Duplicate: If you need to ensure unique elements, use HashSet<T> or SortedSet<T>. For duplicate elements, use List<T> or Dictionary<TKey, TValue>.

  • Key-Value Pairs: If you need to associate values with keys, use Dictionary<TKey, TValue>.

  • LIFO or FIFO: For LIFO behavior, use Stack<T>. For FIFO behavior, use Queue<T>.

Example ๐Ÿ’ก

using System.Collections.Generic;

List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");

Dictionary<int, string> phoneBook = new Dictionary<int, string>();
phoneBook.Add(123456, "Alice");
phoneBook.Add(789012, "Bob");

HashSet<int> uniqueNumbers = new HashSet<int>();
uniqueNumbers.Add(10);
uniqueNumbers.Add(20);
uniqueNumbers.Add(10); // Duplicate, will not be added

foreach (string name in names)
{
    Console.WriteLine(name);
}

Console.WriteLine(phoneBook[123456]);

foreach (int number in uniqueNumbers)
{
    Console.WriteLine(number);
}

By understanding these core concepts and the specific use cases of each collection, you can effectively leverage C#'s powerful collection framework to build robust and efficient applications.

using System.Collections.Generic;

List<string> names = new List<string>();
names.Add("Alice");
names.Add("Bob");
names.Add("Charlie");

Dictionary<int, string> phoneBook = new Dictionary<int, string>();
phoneBook.Add(123456, "Alice");
phoneBook.Add(789012, "Bob");

HashSet<int> uniqueNumbers = new HashSet<int>();
uniqueNumbers.Add(10);
uniqueNumbers.Add(20);
uniqueNumbers.Add(10); // Duplicate, will not be added

foreach (string name in names)
{
    Console.WriteLine(name);
}

Console.WriteLine(phoneBook[123456]);

foreach (int number in uniqueNumbers)
{
    Console.WriteLine(number);
}

By understanding these core concepts and the specific use cases of each collection, you can effectively leverage C#'s powerful collection framework to build robust and efficient applications.

ย