65.9K
CodeProject is changing. Read more.
Home

Generics sample in C#

starIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIconemptyStarIcon

1.39/5 (22 votes)

Aug 6, 2007

1 min read

viewsIcon

67298

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