Collections in C#

Introduction

In C#, collections are used to group and manage a collection of objects. Collections provide various data structures and algorithms to store, retrieve, and manipulate data efficiently. Collections are part of the System.Collections and System.Collections.Generic namespaces in the .NET framework. Here's an overview of some commonly used collections in C#.

  1. Arrays

    • Arrays are fixed-size, contiguous memory structures that store elements of the same data type.
    • They offer fast element access but cannot be resized once created.
    • Example: int[] numbers = new int[5];
  2. Lists (List<T>)

    • Lists are dynamic arrays that automatically resize when needed.
    • They are part of the generic collections and can store elements of a specific type.
    • Example: List<int> numbers = new List<int>();
  3. Dictionaries (Dictionary<TKey, TValue>)

    • Dictionaries store key-value pairs, allowing you to look up values by their associated keys.
    • They are often used for fast data retrieval.
    • Example: Dictionary<string, int> ages = new Dictionary<string, int>();
  4. Queues (Queue<T>)

    • Queues implement a first-in-first-out (FIFO) data structure, where elements are processed in the order they were added.
    • Example: Queue<string> tasks = new Queue<string>();
  5. Stacks (Stack<T>)

    • Stacks implement a last-in-first-out (LIFO) data structure, where the most recently added element is the first to be removed.
    • Example: Stack<int> history = new Stack<int>();
  6. Sets (HashSet<T>)

    • Sets store a collection of unique elements with no particular order.
    • They are useful for eliminating duplicates and checking for membership.
    • Example: HashSet<string> uniqueNames = new HashSet<string>();
  7. Linked Lists (LinkedList<T>)

    • Linked lists are collections of nodes, where each node stores an element and a reference to the next node.
    • They are efficient for insertions and deletions but less efficient for random access.
    • Example: LinkedList<int> linkedList = new LinkedList<int>();
  8. Sorted Collections (SortedDictionary<TKey, TValue>, SortedSet<T>)

    • Sorted collections maintain elements in sorted order based on their keys or values.
    • They provide efficient searching and range-based operations.
    • Example: SortedSet<int> sortedNumbers = new SortedSet<int>();
  9. Read-Only Collections

    • Read-only collections (e.g., ReadOnlyCollection<T>) provide a wrapper around other collections, preventing modifications to the underlying collection.
    • They are useful for exposing collections as read-only properties.
  10. Concurrent Collections

    • Concurrent collections (e.g., ConcurrentDictionary<TKey, TValue>) are designed for multi-threaded scenarios, providing thread-safe operations without the need for external synchronization.
  11. Custom Collections

    • You can create custom collections by implementing the ICollection<T>, IEnumerable<T>, and other related interfaces.

Collections in C# offer a wide range of options for storing and managing data, and the choice of collection type depends on the specific requirements of your application, such as performance, data retrieval patterns, and memory usage.