Generics in C#

Comparison With .NET V1.0

 
Since we are familiar with several types of languages, we all know and have learned about types. But here I am explaining types on the basis of .NET framework. We have the following 2 types:
  • Built-In (int, float, char and so on.)
  • Custom (Shape, Account and so on.)
In .NET framework v 1.0 there were collections, like ArrayList, HashTable and so on. An ArrayList is much like an Array, except very few concepts such as ArrayList could automatically grow and offered many flexible and convenient methods for development that Arrays don’t have.
 
But the problem with ArrayList, HashTable and all other .NET v1.0 collections is that they operate on the type object. Since all the objects are derived from the object type, we can assign anything to an ArrayList or any other collection.
 
The problem with this approach is that we incur performance overhead converting a value type object to and form the object type.
 
 
 
A single ArrayList could hold multiple types that make finding errors at runtime more complex because generally we do write code to handle or to work with only one type, not a set.
 
Generic fixes all these problems.
 

C# | Generic Introduction

 
A Generic is also a collection, but unlike ArrayList, HashTable and all other collections it is strongly typed. A strongly typed collection is type safe, so that we can only put one type of object into it.
 
Some of the most useful features of Generics are:
  • type safety.
  • Eliminates type mismatch at run time.
  • Better performance with value type.
  • Does not incur inter conversion.
With generics we have the most appropriate methods to solve the problem caused by ArrayList. It provides additional functionality without any complexity or problems like with other collections.
 

Generic | Creation of Generic List <T>

 
The procedure for using a generic List collection is similar to the concept of an array. We do declare the List, extend its sub members and then access the extended sub members of that List.
 
For example:
  1. List<int> myInts = new List<int>();   
  2. myInts.Add(1);  
  3. myInts.Add(2);  
  4. myInts.Add(3);  
  5. for (int i = 0; i < myInts.Count; i++)   
  6. {   
  7.    Console.WriteLine("MyInts: {0}", myInts[i]);   
  8. }  

Generic | Handling Dictionary <TKey ,TValue>

 
A Dictionary is nothing but a generic collection that works with key/value pairs. A Dictionary works similarly to the way of non-generic collections such as a HashTable. The only difference between a Dictionary and a HashTable is that it operates on the object type.
 
The complete scenario of a Dictionary collection is explained through the example of customers.
 
For example:
  1. public class Customer  
  2. {   
  3. public    Customer(int id, string name)  
  4.    {   
  5.       ID = id;  
  6.       Name = name;   
  7.    }   
  8.    private int m_id;  
  9.   
  10. public    int ID  
  11.    {   
  12.       get { return m_id; }  
  13.       set { m_id = value; }   
  14.    }   
  15.    private string m_name;   
  16.    public string Name  
  17.    {   
  18.       get { return m_name; }  
  19.       set { m_name = value; }  
  20.    }  
  21. }  

Generic | Dictionary Collection

 
This code part represents dictionary handling with customer extended objects. It also explains how entries are extracted from a Dictionary.
 
For example:
  1. Dictionary<int, Customer> customers = new Dictionary<int, Customer>();   
  2.   
  3. Customer cust1 = new Customer(1, "Cust 1");  
  4. Customer cust2 = new Customer(2, "Cust 2");  
  5. Customer cust3 = new Customer(3, "Cust 3");   
  6. customers.Add(cust1.ID, cust1);  
  7. customers.Add(cust2.ID, cust2);  
  8. customers.Add(cust3.ID, cust3);  
  9.   
  10. foreach (KeyValuePair<int, Customer> custKeyVal in customers)   
  11. {   
  12.    Console.WriteLine(  
  13.    "Customer ID: {0}, Name: {1}",  
  14.    custKeyVal.Key,  
  15.    custKeyVal.Value.Name);   
  16. }  

Referenced Example | Generic

  1. using System;   
  2. using System.Collections.Generic;   
  3. public class Customer  
  4. {   
  5.    public Customer(int id, string name)  
  6.    {   
  7.        ID = id;  
  8.        Name = name;   
  9.     }   
  10.     private int m_id;   
  11.     public int ID  
  12.     {   
  13.        get { return m_id; }  
  14.        set { m_id = value; }   
  15.     }   
  16.     private string m_name;   
  17.     public string Name  
  18.     {   
  19.         get    { return m_name; }  
  20.         set { m_name = value; }   
  21.     }  
  22. }   
  23. class Program  
  24. {  
  25.     static void Main(string[] args)  
  26.     {   
  27.         List<int> myInts = new List<int>();  
  28.         myInts.Add(1);  
  29.         myInts.Add(2);  
  30.         myInts.Add(3);   
  31.         for (int i = 0; i < myInts.Count; i++)  
  32.         {   
  33.             Console.WriteLine("MyInts: {0}", myInts[i]);   
  34.         }   
  35.         Dictionary<int, Customer> customers = new Dictionary<int, Customer>();   
  36.         Customer cust1 = new Customer(1, "Cust 1");  
  37.         Customer cust2 = new Customer(2, "Cust 2");  
  38.         Customer cust3 = new Customer(3, "Cust 3");   
  39.         customers.Add(cust1.ID, cust1);  
  40.         customers.Add(cust2.ID, cust2);  
  41.         customers.Add(cust3.ID, cust3);   
  42.         foreach    (KeyValuePair<int, Customer> custKeyVal in customers)  
  43.         {   
  44.             Console.WriteLine("Customer ID: {0}, Name: {1}", custKeyVal.Key, custKeyVal.Value.Name);  
  45.         }  
  46.         Console.ReadKey();  
  47.     }  
  48. }


Similar Articles