C# Collection

Collection in C#

A C# collection is used to group related objects. Unlike an array, it is dynamic and can also group objects. A collection can grow and shrink to accommodate any number of objects. Collection classes are organized into namespaces and contain built in methods for processing elements within the collection.

A collection organizes related data in a computer so that it can be used efficiently.


Generic C# Collection :

Generic collections are the preferred type to use as long as every element in the collection is of the same data type. Only desired data types can be added to a generic collection .

The .NET Framework provides a number of generic collection classes, useful for storing and manipulating data.


The System.Collections.Generic namespace includes the following generic collections:

  1. List<T>
  2. Dictionary<TKey, TValue>
  3. SortedList<TKey, TValue>
  4. Stack<T>
  5. Queue<T>
  6.  Hashset<T>

Non-Generic C# Collection:

Non-generic collections can store items that are of type Object. Since an Object data type can refer to any data type, you run the risk of unexpected outcomes. Non-generic collections may also be slower to access as well as execute.

The System.Collections namespace includes the following non-generic collections:

  1. ArrayList
  2. SortedList
  3. Stack
  4. Queue
  5. Hashtable
  6. BitArray

Various Collection Classes and Their Usage

Sr.No.ClassDescriptionUsage
1.ArrayListIt represents ordered collection of an object that can be indexed individually.It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.
2.HashtableIt uses a key to access the elements in the collection.A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.
3.SortedListIt uses a key as well as an index to access the items in a list.A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value.
4.StackIt represents a last-in, first out collection of object.It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.
5.QueueIt represents a first-in, first out collection of object.It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.
6.BitArrayIt represents an array of the binary representation using the values 1 and 0.It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.

Recommended Posts:

Spread the love

Leave a Comment

Your email address will not be published. Required fields are marked *