BCA / B.Tech 13 min read

Generic Collections in .NET (C#)

Generic Collections in .NET (C#) | Generic Collections in .NET in Hindi


What are Generic Collections?

  • Generic Collections are data structures that are type-safe and can store elements of any type (data type),
  • without having to worry about what type of data (like int, string, etc.) they will be used as.
  • The main advantage of this is that you can ensure at compile-time that the wrong type of data does not get into the collection.
  • For example, if we create a list that will only store integer (int) values, Generic Collections help us to ensure
  • that only int is put into that list and no other type of data like string, float, etc. gets into it.
  • Generics are powerful and flexible tools in .NET (C#) that help us to create type-safe, reusable, and performance-friendly collections.
  • Collections like List<T> and Dictionary<TKey, TValue> use generics, which give you the facility to work with different types of data.
What can Generics do for us?

We can get some important benefits from the use of generics:

  • Type Safety: With generics, we can do type checking at compile-time, which means you will have fewer run-time errors. For example, if you are creating a List<int>, no other type besides int can go into it.
  • Code Reusability: Generics give us the facility to use the same data structure with many types of data. This makes our code more reusable and maintainable.
  • Performance Improvement: Generics save us from problems like boxing and unboxing, which occur in non-generic collections. This improves performance because the data is stored in its original type.
  • Generic List<T> Collection: List<T> is a generic collection that stores data of any type in the form of a list. Here T represents the data type.

Example:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // List of Integer type
        List<int> numbers = new List<int>();

        // Add values to the List
        numbers.Add(10);
        numbers.Add(20);
        numbers.Add(30);

        // Print each element of the List
        foreach (int number in numbers)
        {
            Console.WriteLine(number);
        }
    }
}

In this, List<int> is a generic list that can only store data of integer (int) type. You can also create it for any other type like string, double, etc., such as:

List<string> names = new List<string>();
names.Add("John");
names.Add("Sara");
Dictionary<TKey, TValue> Collection

Dictionary<TKey, TValue> is a generic collection that stores key-value pairs. In this, TKey is the type that represents the key, and TValue is the type that represents the value. In a Dictionary, every key has a unique value, which is associated with it.

Example:

using System;
using System.Collections.Generic;

class Program
{
    static void Main()
    {
        // Dictionary, where key type is string and value type is int
        Dictionary<string, int> ageDictionary = new Dictionary<string, int>();

        // Add data to the Dictionary
        ageDictionary.Add("John", 25);
        ageDictionary.Add("Sara", 30);
        ageDictionary.Add("Mike", 35);

        // Print all key-value pairs of the Dictionary
        foreach (var pair in ageDictionary)
        {
            Console.WriteLine($"Name: {pair.Key}, Age: {pair.Value}");
        }
    }
}

In this, Dictionary<string, int> is a dictionary where the key is of string type (person's name) and the value is of int type (person's age). You can also use it for any other type of data such as:

Dictionary<int, string> empDict = new Dictionary<int, string>();
empDict.Add(1, "John");
empDict.Add(2, "Sara");

Advantages of Generics in Hindi | Advantages of Generics:

  • Type Safety: As mentioned earlier, Generics perform type checking at compile-time, which reduces runtime errors.
  • Performance: Non-generic collections have boxing and unboxing (when value types have to be converted to reference types), which affects performance. Generics avoid these problems.
  • Reusable Code: Once you create a generic class or method, you can reuse it for different types of data, without the need to write a new class every time.