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:
List<int> myInts = new List<int>();
myInts.Add(1);
myInts.Add(2);
myInts.Add(3);
for (int i = 0; i < myInts.Count; i++)
{
Console.WriteLine("MyInts: {0}", myInts[i]);
}
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:
public class Customer
{
public Customer(int id, string name)
{
ID = id;
Name = name;
}
private int m_id;
public int ID
{
get { return m_id; }
set { m_id = value; }
}
private string m_name;
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
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:
Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
Customer cust1 = new Customer(1, "Cust 1");
Customer cust2 = new Customer(2, "Cust 2");
Customer cust3 = new Customer(3, "Cust 3");
customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust2);
customers.Add(cust3.ID, cust3);
foreach (KeyValuePair<int, Customer> custKeyVal in customers)
{
Console.WriteLine(
"Customer ID: {0}, Name: {1}",
custKeyVal.Key,
custKeyVal.Value.Name);
}
Referenced Example | Generic
using System;
using System.Collections.Generic;
public class Customer
{
public Customer(int id, string name)
{
ID = id;
Name = name;
}
private int m_id;
public int ID
{
get { return m_id; }
set { m_id = value; }
}
private string m_name;
public string Name
{
get { return m_name; }
set { m_name = value; }
}
}
class Program
{
static void Main(string[] args)
{
List<int> myInts = new List<int>();
myInts.Add(1);
myInts.Add(2);
myInts.Add(3);
for (int i = 0; i < myInts.Count; i++)
{
Console.WriteLine("MyInts: {0}", myInts[i]);
}
Dictionary<int, Customer> customers = new Dictionary<int, Customer>();
Customer cust1 = new Customer(1, "Cust 1");
Customer cust2 = new Customer(2, "Cust 2");
Customer cust3 = new Customer(3, "Cust 3");
customers.Add(cust1.ID, cust1);
customers.Add(cust2.ID, cust2);
customers.Add(cust3.ID, cust3);
foreach (KeyValuePair<int, Customer> custKeyVal in customers)
{
Console.WriteLine(
"Customer ID: {0}, Name: {1}",
custKeyVal.Key,
custKeyVal.Value.Name);
}
Console.ReadKey();
}
}