Click here to Skip to main content
15,891,748 members
Articles / Programming Languages / C#
Article

Generics sample in C#

Rate me:
Please Sign up or sign in to vote.
1.39/5 (35 votes)
5 Aug 20071 min read 66.6K   14   13
This article will explain about Generics feature in C# 2.0

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;

   }

}

 

// Main class as follows:

 
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....

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
India India
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.

Comments and Discussions

 
GeneralMy vote of 1 Pin
raoraorao12-Aug-14 17:37
raoraorao12-Aug-14 17:37 
GeneralMy vote of 1 Pin
Ashish_2319-Oct-12 9:50
Ashish_2319-Oct-12 9:50 
Question[My vote of 1] please don't put this type of wrong examples. Pin
Ashish_2319-Oct-12 9:49
Ashish_2319-Oct-12 9:49 
GeneralMy vote of 2 Pin
UllasN6-Sep-12 0:26
UllasN6-Sep-12 0:26 
GeneralMy vote of 1 Pin
Member 80936599-Jun-12 8:39
Member 80936599-Jun-12 8:39 
GeneralMy vote of 1 Pin
Senthilkumar Elangovan14-Mar-12 8:21
Senthilkumar Elangovan14-Mar-12 8:21 
GeneralMy vote of 1 Pin
Member 266051023-Sep-11 3:45
Member 266051023-Sep-11 3:45 
GeneralDifferences between method overloading and Generics Pin
SRKVELLANKI20-Apr-09 8:14
SRKVELLANKI20-Apr-09 8:14 
Generalவணக்கம்... Pin
Thamizhan16-May-08 4:01
Thamizhan16-May-08 4:01 
GeneralGood to have this article Pin
David Cinda16-May-08 3:53
David Cinda16-May-08 3:53 
GeneralVery lightweight Pin
Pete O'Hanlon6-Aug-07 1:30
mvePete O'Hanlon6-Aug-07 1:30 
GeneralTrash! Pin
Joey Chömpff6-Aug-07 1:23
Joey Chömpff6-Aug-07 1:23 
QuestionGood practice? Pin
loneferret6-Aug-07 1:16
loneferret6-Aug-07 1:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.