Thursday, June 23, 2005

Generics in Visual Studio.NET 2005

Features Of Visual Studio 2005.NET Beta 2:
Struck by the sheer amount of time some of us have wasted by not playing around with Visual Studio.NET 2005!!! For the next few days I'll be publishing all new features of Visual Studio.NET 2005 as I play around with them. After installing Visual Studio.NET 2005 (and NOT being able to install MSDN Locally!) Generic Classes was the first thing I could lay my eyes on. I quickly wanted to write up a Generic Class and understand what I can and cannot do with them! The Below code snippet attempts to describe:
When Would I use Generic Classes?
From what I gathered I Would use Generics when I want a Type to accept any Generic Type and perform action on it! This requires some code to explain (the MSDN Code of Linked List implementation was slightly complicated):

/// <summary>
// Takes Any Generic Class.
/// </summary>
/// <typeparam name="T"></typeparam>
public class CurrentObject<T>
{
private T data;
/// <summary>
/// return the added object of the class to the calling program.
/// </summary>
/// <returns></returns>
public T returnObject()
{
return data;
}
/// <summary>
/// Allows addition of object of that Generic class
/// </summary>
/// <param name="objectData"></param>
public void AddObject(T objectData)
{
data = objectData;
}
}
 

The first thing we do in the above snippet is Create a class called CurrentObject which can accept any Generic Class as a parameter. Once we have accepted a type we create an object of the type accepted in the first line of the class.
We now introduce two new methods returnObject and AddObject. AddObject allows me to pass an object of the class I declared earlier. Once we have the object we just assign it to the object we created internally in the CreateObject class. In return Object we just return the class.
Lets take a quick preview of how we use this Generic Class. First thing we do is we create a quick class with two field that we will use with this Generic Class that we created!

/// <summary>
/// A Class That Will be passed to the Current Object object
/// </summary>
public class Customer
{
public string CustomerFirstName = "";
public string CustomerLastName = "";
}
Now the interesting part:
static void Main(string[] args)
{
// Create and Instance Of CurrentObject Generic Type
CurrentObject<Customer> currentCustomer = new CurrentObject<Customer> ();
Customer tmpCustomer = new Customer();
tmpCustomer.CustomerFirstName = "rajiv";
tmpCustomer.CustomerLastName = "popat";
// The Below could be a complex Linked List or Similar Implementation!
currentCustomer.AddObject(tmpCustomer);
Customer finalCustomer = currentCustomer.returnObject();
// Print the results obtained from the Generic type!
Console.WriteLine("Customer First Name is : " + finalCustomer.CustomerFirstName);
Console.WriteLine("Customer Last Name is : " + finalCustomer.CustomerLastName);
Console.ReadKey();

}

We create an object of CurrentObject Generic class by passing the Customer class to it. Then we use another customer object that we have and pass it to the AddObject Method. (This is the method which could have been as complicated as we want based on the logic of the collection that we are developing!) The returnObject finally returns the object and assigns it to finalCustomer. Again, it is to be noted here returnObject could return any object of the same class based on the complex logic of our collection!!!

So basically the same collection we design works for virtually ALL classes and types!!! MIND BLOWING!!!

2 Comments:

Anonymous Anonymous said...

Are these are mostly used for custom collections or there are more uses you thought about?

2:13 AM  
Blogger rajivpopat said...

That got me thinking and I posted another blog for it called "Thinking about generics".

Thanks for the interesting question :)

7:57 AM  

Post a Comment

<< Home