Drawbacks of using non-generic
If you use an arraylist to create say an array of int and another array list to create an array of strings. What happens internally is that the array elements are converted into object. If they are value type they are converted using boxing, and they are retrieved using unboxing. This boxing, unboxing creates an overhead and degrades performance. The other limitation is lack of compile-time type checking; since an ArrayList casts everything to Object, there is no way at compile-time to check data types.
Using Generics
By using generics you can specify the type of the collection while creating its object itself. This allows the compiler to check whether the type of elements are correct at compile time only. Creating and using generics
They are in the namespace System.Collections.Generic;
Stack<int> myStk = new Stack<int>();
myStk.Push(1);
myStk.Push(2);
myStk.Push(3);
myStk.Push(4);
Push and Pop operations are available. Here is you try to push a string, like, myStk.Push("abc");, compiler will throw an error as myStk is an integer stack.
You can use stack.Pop method to pop the elements as given below:
while (myStk.Count>0)
{
TextBox1.Text += myStk.Pop().ToString() + " ";
}
Apart from this Generics have Enumerator to iterate through the stack elements. The enumerator objects just iterates through the stack, without deleting the stack objects. Note: After creating Enumerator object do not change the collection.
Stack<int>.Enumerator myEnum = myStk.GetEnumerator();
while (myEnum.MoveNext())
{
TextBox1.Text += myEnum.Current.ToString() + "-";
}
If you use an arraylist to create say an array of int and another array list to create an array of strings. What happens internally is that the array elements are converted into object. If they are value type they are converted using boxing, and they are retrieved using unboxing. This boxing, unboxing creates an overhead and degrades performance. The other limitation is lack of compile-time type checking; since an ArrayList casts everything to Object, there is no way at compile-time to check data types.
Using Generics
By using generics you can specify the type of the collection while creating its object itself. This allows the compiler to check whether the type of elements are correct at compile time only. Creating and using generics
They are in the namespace System.Collections.Generic;
Stack<int> myStk = new Stack<int>();
myStk.Push(1);
myStk.Push(2);
myStk.Push(3);
myStk.Push(4);
Push and Pop operations are available. Here is you try to push a string, like, myStk.Push("abc");, compiler will throw an error as myStk is an integer stack.
You can use stack.Pop method to pop the elements as given below:
while (myStk.Count>0)
{
TextBox1.Text += myStk.Pop().ToString() + " ";
}
Apart from this Generics have Enumerator to iterate through the stack elements. The enumerator objects just iterates through the stack, without deleting the stack objects. Note: After creating Enumerator object do not change the collection.
Stack<int>.Enumerator myEnum = myStk.GetEnumerator();
while (myEnum.MoveNext())
{
TextBox1.Text += myEnum.Current.ToString() + "-";
}
No comments:
Post a Comment