Note - Please ignore all the single quotes, put for some technical reason.
A generics is class, method, interface or structure that have place holder for types parameter.
A generic class has type parameter for its fields, methods’ parameters.
A method has type parameter for return type or one of its formal parameters.
Storing value / reference type or different data types in a single list(collection) achievable with ‘ArrayList’.
ArrayList lst1 = new ArrayList();
Lst1.add(10);
Lst1.add(150);
Lst1.add(“Red”);
Lst1.add(“Blue”);
But it comes at a cost of type conversion and not type safe also.
Every time when we add a value type to the list, boxing needed and when we retrieve it from list, unboxing needed, all it done implicitly.
Generics introduced with framework 2.0 that can achieve it without type conversion and also type safe.
Below given class definition can be used with any data type.
Class Myclass
{
}
Myclass’<
Myclass
Namespace System.Collection.Generic provides some collection classes those support Generics.
Like Queue, Stake, List, Linked list etc.
Benefit : Code re-usability, type safe, no type casting required and efficient.
Terminology
1. Generic type definitions are templates you can’t instantiate the template, you must specify type at the time of instantiation.
2. Generic type parameters are place holders for types.
3. When we create instance of generic type definition, we specify type and the instance called ‘constructed type’ or ‘constructed generic type’.
4. General term generic type contains both ‘generic type definition’(template) and ‘generic constructed type’(instance).
5. A generic method has two parameter lists one is generic type and another is formal type.
T MyMethod1(T t) //Not a generic method
{
T temp = t;
Return temp;
}
A generic method must has its own generic types list.
T MyMethod2 <’T’>
{
T temp = t;
Return temp;
}
Class myClass <’T’>
{
T MyMethod1(T t)
{
T temp = t;
Return temp;
}
}
For integers you can implement and use the IntStack:
public class IntStack
{
int[] m_Items;
public void Push(int item){...}
public int Pop(){...}
}
IntStack stack = new IntStack();
stack.Push(1);
int number = stack.Pop();
For strings you would implement the StringStack:
public class StringStack
{
string[] m_Items;
public void Push(string item){...}
public string Pop(){...}
}
StringStack stack = new StringStack();
stack.Push("Ram");
string name = stack.Pop();
//Using Generics we can make above approach with code reusability
public class Stack'<'T'>
{
T[] m_Items;
public void Push(T item)
{...}
public T Pop()
{...}
}
Stack'<'int'>
stack.Push(2);
int number = stack.Pop();
Stack'<'string'>
stack.Push("Shyam");
string name = stack.Pop();
Q. How Generics are type-safe?
Ans. A Generic type is a template, we specify type at the time of instantiation then you can add or assign items of only specified type to that instance, can't be added any type other than specified type, if you attempt to add any other type code will not compile .
===================
Ex.
public class Lineclass<'T'>
{
public T LineMethod<'T'>
{
T Temp = t; return Temp;
}
internal string convt<'T'>
{
T temp = Tnt; return temp.ToString();
}
public void main()
{
Lineclass<'double'>
Response.Write(lc.convt(lc.LineMethod(500.00)));
}
}
| Constraint | Description |
| Where T : struct | Type argument must be value type and not nullable. |
| where T : class | Type argument must be reference type, it can be class, interface, array, delegate. |
| where T : new() | Type argument must have a parameter less public constructor. If you are using this constraints with other then it must specified at last. |
| where T : | Type argument must be given base type or derived from given base type. |
| where T : | Type argument must be given interface type or type that implemented given interface. Multiple interface constraints can also be defined. Supplied interface type can be generic type. |
| where T : U | Type argument must be type or derived type of type supplied for generic U |
No comments:
Post a Comment