How to use Generics in DOT NET 2.0
Hi

In Dot Net 1.X developers had to use System.Collections non-Generic collections for their Objects and hence had no type checking. So the developer would have to do type casting while retrieving the objects. One of the main new features of DOT NET (Generics) has made life easier for the developers. Another important reason for using Generics against Non Generics in performance. Reading objects from and Array List will have better performance since we will not have to type cast the Object. Generics also gives us compile time Type safety.

So how does Generics change our code?

In Dot Net 1.x Array list was one of the most common to store objects since it can save any collection regardless of their type. We used to type cast the objects when we used to retrieve them.

List<T> can be used as a good alternative to Array List. The main difference is that List<T> is type specific.

So how does our code change? Here is a look at the change in code.

Let’s start by creating a simple class Test. The Test class has four properties

class Test
    {
        private int _TestID;
        private bool _Active;
        private string _TestDesc;
        private string _TestName;
        public int TestID
        {
            get { return _TestID; }
            set { _TestID = value; }
        }
        public string TestName
        {
            get { return _TestName; }
            set { _TestName = value; }
        }
        public string TestDesc
        {
            get { return _TestDesc; }
            set { _TestDesc = value; }
        }
        public bool Active
        {
            get { return _Active; }
            set { _Active = value; }
        }
    }
 

This class have four Properties. Lets Add some data to the class. I am using some dummy values to add data

Here is the code for adding Data in Dot Net 1.x

private void ProcessArrayList()
{
    ArrayList ListArray = new ArrayList();
    Random rand = new Random();   
    bool act = true;
    for (int i = 0; i < 10000; i++)
    {
        Test Test = new Test();
        int id = rand.Next(10000);
        if (act == true)
        { act = false; }
        else
        { act = true; }
        Test.ID = id;
        Test.Name = "Test #" + id.ToString();
        Test.TestDesc = "TestDesc:" + id.ToString();
        Test.Active = act;
        ListArray.Add(Test);
    }
}

Here is the code for adding Data in Dot Net 2.0

private void ProcessLISTT()
{
    List<Test> ListT = new List<Test>();
    Random rand = new Random();
    bool act = true;
    for (int i = 0; i < 10000; i++)
    {
        Test Test = new Test();
        int id = rand.Next(10000);
        if (act == true)
        { act = false; }
        else
        { act = true; }
        Test.ID = id;
        Test.Name = "Test #" + id.ToString();
        Test.TestDesc = "TestDesc:" + id.ToString(); 
        Test.Active = act;
        ListT.Add(Test);
    }
} 

You will find the code is very much the same. The difference is only in creating the List Object;

Now to retrieve data from the list in Dot Net 1.X

private void ArrayListMethod(ArrayList List)

{
    foreach (Test Test in List)
    {
        ////Do something with the data
    }
}

In dot Net 2.0

private void ListOfTMethod(List<Test> List)
{
    foreach (Test Test in List)
    {
        ////Do something with the data
    }
}

Hope this helps
Thanks
Vikram


Share this post   Email it

Feedback

Posted on 2/3/2008 11:39:19 PM

Please tell me the Basics of API and how do i use that in my application.Actually I need to know how the transactions are made using CXML and asp.net using Apis

It's Urgent
plz Help

Posted on 2/4/2008 7:15:43 AM

Hi lakshmi

I am sorry to say I do not have much idea on that. I would recommend you to go to forums.asp.net for the query

Please post your comments:

Name:  
Email (optional): Your email address will not be posted.
URL (optional):
Comments: HTML will be ignored, URLs will be converted to hyperlinks  
Enter the text you see in the box:
 

Copyright © 2006 - 2012 Vikram Lakhotia