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?/p>
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./p>
List<T>
can be used as a good alternative to Array List. The main difference is that
List<T> is type specific. /p>
So
how does our code change? Here is a look at the change in code./p>
Let’s
start by creating a simple class Test. The Test class has four properties/p>
/o:p>class Test/o:p>
/span>{/o:p>
/span>private int _TestID;/o:p>
/span>private bool _Active;/o:p>
/span>private string _TestDesc;/o:p>
/span>private string _TestName;/o:p>
/span>public int TestID/o:p>
/span>{/o:p>
/span>get { return _TestID; }/o:p>
/span>set { _TestID = value; }/o:p>
/span>}/o:p>
/span>public string TestName/o:p>
/span> /span>{/o:p>
/span>get { return _TestName;
}/o:p>
/span>set { _TestName = value;
}/o:p>
/span>}/o:p>
/span>public string TestDesc/o:p>
/span>{/o:p>
/span>get { return _TestDesc;
}/o:p>
/span>set { _TestDesc = value;
}/o:p>
/span>}/o:p>
/span>public bool Active/o:p>
/span> /span>{/o:p>
/span>get { return _Active; }/o:p>
/span>set { _Active = value; }/o:p>
/span>}/o:p>
/span>}/o:p>/span>
/o:p>/p>
This
class have four Properties. Lets Add some data to the class. I am using some
dummy values to add data/p>
Here
is the code for adding Data in Dot Net 1.x /p>
private void ProcessArrayList()/o:p>
{/o:p>
/span>ArrayList ListArray = new
ArrayList();/o:p>
/span>Random rand = new Random(); /span>/o:p>
/span>bool act = true;/o:p>
/span>for (int i = 0; i < 10000;
i++)/o:p>
/span>{/o:p>
/span>Test Test = new Test();/o:p>
/span>int id = rand.Next(10000);/o:p>
/span>if (act == true)/o:p>
/span>{ act = false; }/o:p>
/span>else/o:p>
/span>{ act = true; }/o:p>
/span>Test.ID = id;/o:p>
/span>Test.Name = "Test
#" + id.ToString();/o:p>
/span>Test.TestDesc =
"TestDesc:" + id.ToString();/o:p>
/span>/o:p>/span> Test.Active = act;/o:p>
/span>ListArray.Add(Test);/o:p>
/span> /span>}/o:p>
}/o:p>/span>/p>Here
is the code for adding Data in Dot Net 2.0 /p>
private void ProcessLISTT()/o:p>
{/o:p>
/span>List<Test> ListT = new
List<Test>();/o:p>
/span>Random rand = new Random();/o:p>
/span>bool act = true;/o:p>
/span>for (int i = 0; i < 10000;
i++)/o:p>
/span>{/o:p>
/span>Test Test = new Test();/o:p>
/span> /span>int id = rand.Next(10000);/o:p>
/span>if (act == true)/o:p>
/span>{ act = false; }/o:p>
/span>else/o:p>
/span>{ act = true; }/o:p>
/span>Test.ID = id;/o:p>
/span>Test.Name = "Test
#" + id.ToString();/o:p>
/span>Test.TestDesc =
"TestDesc:" + id.ToString(); /o:p>
/span>Test.Active = act;/o:p>
/span>ListT.Add(Test);/o:p>
/span>}/o:p>
}/o:p>/span> /o:p>
You
will find the code is very much the same. The difference is only in creating
the List Object;/p>
Now
to retrieve data from the list in Dot Net 1.X/p>
/o:p>private void ArrayListMethod(ArrayList List)/o:p>/span>/p>
{/o:p>
/span>foreach (Test Test in List)/o:p>
/span>{/o:p>
/span>////Do something with the data/o:p>
/span>}/o:p>
}/o:p>/span>/p>
/o:p>/span>In
dot Net 2.0/p>
/o:p>private void ListOfTMethod(List<Test> List)/o:p>
{/o:p>
/span>foreach (Test Test in List)/o:p>
/span>{/o:p>
/span>////Do something with the data/o:p>
/span>}/o:p>
}/span>/p>Hope
this helps
Thanks
Vikram/p>