What are Collections in C# and what are their types?

C# collection are used to store, manage and manipulate data.  

collections are data structures or containers that are used to store, organize, and manipulate groups of related objects or values. They provide convenient ways to manage and work with collections of data in a structured manner. The .NET Framework provides various collection classes that offer different functionalities and usage scenarios.


For example ArrayList, Dictionary, List, Hashtable etc.

Here are some commonly used collection types in C#:

List<T>:

  • List is a dynamic, ordered collection that can grow or shrink in size.
  • It allows duplicate values and preserves the insertion order of elements.
  • Elements in a list can be accessed by their index.
  • Example: List<T>

Dictionary<TKey, TValue>:

  • Dictionary is a collection of key-value pairs, where each key is unique and associated with a value.
  • It provides fast lookup and retrieval of values based on keys.
  • Keys in a dictionary are used to access the corresponding values.
  • Example: Dictionary<TKey, TValue>

HashSet<T>:

  • HashSet is an unordered collection that stores unique elements.
  • It does not allow duplicate values.
  • Elements in a HashSet can be quickly accessed and provide efficient set-based operations.
  • Example: HashSet<T>

Queue<T>:

  • Queue is a collection that follows the First-In-First-Out (FIFO) principle.
  • Elements are added at the end and removed from the beginning of the queue.
  • Example: Queue<T>

Stack<T>:

  • Stack is a collection that follows the Last-In-First-Out (LIFO) principle.
  • Elements are added and removed from the top of the stack.
  • Example: Stack<T>
LinkedList<T>:
  • LinkedList is a collection that stores elements as nodes, with each node containing a reference to the next and/or previous node.
  • It allows efficient insertion and removal of elements at any position in the collection.
  • Example: LinkedList<T>

These are just a few examples of the collection types available in C#. Each collection type serves a specific purpose and provides different functionalities to suit different data manipulation scenarios. Choosing the appropriate collection type depends on the specific requirements of your application, such as the desired behavior, performance characteristics, and data access patterns.

Post a Comment

Previous Post Next Post