Introduction
This article is going to deal with Generics feature in C# 2.0. Generics is a new feature added in C# 2.0 language and the Common Language Runtime (CLR). We can say it like Parametric Polymorphism. Generics allow us to create type safe collections with no boxing and unboxing overhead.
Background
As we know C# is a strongly typed language, programmers have to specify the type of an object at the time of writing the program.
"Generics introduce to the .NET Framework the concept of type parameters, which make it possible to design classes and methods that defer the specification of one or more types until the class or method is declared and instantiated by client code."
We can write a generic method for sorting an array of objects, then invoke the generic method separately with an int array, a double array, a string array and so on, to sort each different type of array.
Generic methods enable us to specify, with a single method declaration, a set of related methods. Generic classes enable us to specify, with a single class declaration, a set of related classes. Similarly, generic interfaces enable us to specify, with a single interface declaration, a set of related interfaces. Generics provide compile-time type safety.
Using the code
I tried with our simple address book example in this article. Address class contains properties of DoorNo (int), StreetName (string), CityName (string), and PhoneNumber (int). In my implementation part, I may use all this properties to the object or some may be missed out:
class Address
{
int _DoorNo;
public int DoorNo
{
get { return _DoorNo; }
set { _DoorNo = value; }
}
string _StreetName;
public string StreetName
{
get { return _StreetName; }
set { _StreetName = value; }
}
string _City;
public string City
{
get { return _City; }
set { _City = value; }
}
string _State;
public string State
{
get { return _State; }
set { _State = value; }
}
int _Phone;
public int PhoneNo
{
get { return _Phone; }
set { _Phone = value; }
}
public Address() { }
public Address(int DoorNumber, string StreetName, string CityName)
{
this.DoorNo = DoorNumber;
this.StreetName = StreetName;
this.City = CityName;
this.PhoneNo = 0;
}
public Address(int DoorNumber, string StreetName, string CityName, int PhoneNumber)
{
this.DoorNo = DoorNumber;
this.StreetName = StreetName;
this.City = CityName;
this.PhoneNo = PhoneNumber;
}
}
class Program
{
static void Main(string[] args)
{
List<Address> myAddress = new List<Address>();
myAddress.Add(new Address(1,"New Street", "Chennai"));
myAddress.Add(new Address(2, "Second Main Road, TNHB", "Chennai", 866413553));
myAddress.Add(new Address(3, "New Street", "Bangalore"));
myAddress.Add(new Address(4, "Second Main Road, TNHB", "Bangalore", 885634367));
myAddress.Add(new Address(5, "New Street", "Pune"));
myAddress.Add(new Address(6, "Second Main Road, TNHB", "Pune", 433243664));
myAddress.Add(new Address(7, "New Street", "Gurgaon"));
myAddress.Add(new Address(8, "Second Main Road, TNHB", "Gurgaon", 564778634));
foreach (Address a in myAddress)
{
Console.WriteLine("New Address Entry follows: \n");
Console.WriteLine("Door Number : " + a.DoorNo);
Console.WriteLine("Street Name :" + a.StreetName);
Console.WriteLine("City :" + a.City);
Console.WriteLine("Phone Number:" + a.PhoneNo+"\n\n");
}
Console.ReadLine();
}
}
History
This article series will continue with new features of C# 2.0. Wait till my next post...
Cheers.... Happy coding....
Working as Associate Consultant with MindTree Consulting Ltd., Chennai since April 2007.
Was working with Verizon Data Services India Pvt Ltd., Chennai as Senior Analyst from June 2005 to April 2007, with Caravel Info Systems Pvt., Bangalore as Systems Engineer from November 2003 to June 2005, and with TexArt Solutions, Karur as Software Engineer from April 2001 to November 2003.
Worked E-Learning, Tele-communication, and Travel domain projects using C#.NET, ASP.NET and JavaScript with SQL Server 2000.
Willing to work with future .NET concpets and make it easy to the public.