This Feature was introduced In C# 2.0
What is a Collection?
A collection Sometimes Called as Container is an object That Groups multiple Elements into a single unit
So Why use collections
Collections Reduces Programming Efforts as it helps provides you with useful data structure and algorithms
Consider an example of a stack and the two methods of push and pop in .net in C# 1.1 it provided you a stack of an object type as object is the base class in C# you were able to put anything into the stack See the Example Below:
public class Stack
{
object[] Items;
public void Push(object item)
{...}
public object Pop()
{...}
}
Stack stack = new Stack();
stack.Push("One");
stack.Push("Two");
string number = (string)stack.Pop();
The problem here was that in case of value types you need to box it to object type
and then again unbox it boxing and unboxing had a performance issue but not only that
Consider the below example:
Stack stack = new Stack();
stack.Push("One");
stack.Push("Two");
int number = (string)stack.Pop(); //This will throw a runtime exception
To overcome this problem there was a solution to create a Specific type Stack
For eg:IntStack to store int and String stack to store string and so on
But the problem here was that if i had to change the algorithm or if there were any
error in the algorithm i had to make this change in all the Class files which would be tedious and repetitive work So Generics Were introduced
Generics allows you to define type-safe classes without compromising type safety, performance, or productivity.
for example consider the example given below
public class Stack<T>
{
T[] Items;
public void Push(T item)
{...}
public T Pop()
{...}
}
Stack<int> stack = new Stack<int>();
stack.Push(1);
stack.Push(2);
int number = stack.Pop();
so now when you are creating a stack you need to specify the type both when declaring the variable and when instantiating it:
Stack<int> stack = new Stack<int>();
so in the above example the generic T is replaced with the type specific data type and also you can reuse the code for any other type and if you need to change the algo you had to now change in at just one place
But now if you consider generics There is still a constrain as in generics the type is defined at compile time so if i have to do some manipulation over it it used to provide me an error
see the below example
public T Find(K key)
{
Stack<T> current = null;
while (Items != null)
{
if (Items == key) //Will not compile
{
current = Items;
break;
}
}
return current;
}
because it does not know weather the type specified by the client supports the equals operator now in order to solve this problem you had to specify it like this
public class Stack<T> where T : IComparable
{
T[] Items;
public void Push(T item)
{...}
public T Pop()
{...}
public T Find(K key)
{
Stack<T> current = null;
while (Items != null)
{
if (Items == key) //Will not compile
{
current = Items;
break;
}
}
return current;
}
}
just as a class can be of generic you can also create a method as generic consider the
example below
public void MyMethod<X>(X x)
{...}
About Author:
Steven Pinto is technology geek and loves to write on technology. He works in Systems Plus Pvt. Ltd. and actively contributes to technology.
To more of interesting topics written by Steven, follow http://mad4teck.blogspot.in/
Steven Pinto is technology geek and loves to write on technology. He works in Systems Plus Pvt. Ltd. and actively contributes to technology.
To more of interesting topics written by Steven, follow http://mad4teck.blogspot.in/
No comments:
Post a Comment