Overview:
Example:- Generic classes have type parameters.
- It is type independent because we can use any type.
- The letter T denotes a type.
- Instance of T like it is a real type, but it is not.
- It helps us to create strong type in compile time, code reuse and performance.
- Example 01 :
{
T _value;
public GenericClass(T valueParam)
{
this._value = valueParam;
}
public void Write()
{
Console.WriteLine(this._value);
}
}
class Program
{
static void Main(string[] args)
{
GenericClass<int> objOne = new GenericClass<int>(1);
objOne.Write();
GenericClass<string> objTwo = new GenericClass<string>("Saniul Naim");
objTwo.Write();
Console.ReadLine();
}
}
Output: 1
Saniul Naim
- Example 02 :
public struct Point<T>
{
public T x;
public T y;
}
class Program
{
static void Main(string[] args)
{
Point<double> decimalOjbWithInstatiation=new Point<double>();
decimalOjbWithInstatiation.x = 3.3;
decimalOjbWithInstatiation.y = 2.1;
Console.WriteLine("x={0},y={1}", decimalOjbWithInstatiation.x, decimalOjbWithInstatiation.y);
Point<int> decimalOjbWithoutInstatiation;
decimalOjbWithoutInstatiation.x = 3;
decimalOjbWithoutInstatiation.y = 2;
Console.WriteLine("x={0},y={1}",decimalOjbWithoutInstatiation.x, decimalOjbWithoutInstatiation.y);
Console.ReadLine();
}
}
Output: x=3.3,y=2.1
x=3,y=2
Note: Notice that first we instantiate Point class but second time is not. But both two is working well. So what's the different between two!!!???
Ans: In brief, here Struct is immutable so if we need to zero the memory we need instantiate.
- Example 01 :
public class MultipleGenericClass<T, K>
{
private T tValue;
private K kValue;
public MultipleGenericClass(T tParam, K kParam)
{
tValue = tParam;
kValue = kParam;
}
public void Write()
{
Console.WriteLine("{0}{1}", tValue, kValue);
}
}
{
private T tValue;
private K kValue;
public MultipleGenericClass(T tParam, K kParam)
{
tValue = tParam;
kValue = kParam;
}
public void Write()
{
Console.WriteLine("{0}{1}", tValue, kValue);
}
}
class Program
{
static void Main(string[] args)
{
MultipleGenericClass<string, int> multipleGenericClassObj = new MultipleGenericClass<string, int>("My age is ", 25);
multipleGenericClassObj.Write();
Console.ReadLine();
}
}
{
static void Main(string[] args)
{
MultipleGenericClass<string, int> multipleGenericClassObj = new MultipleGenericClass<string, int>("My age is ", 25);
multipleGenericClassObj.Write();
Console.ReadLine();
}
}
Output: My age is 25
Generic class Constrain:
- Here type T is fixed such as struck, class etc.
- We can fixed type by 'where' condition such as 'where T : struck'.
- Example 01 :
public class DisposableGenericClass<T> where T :IDisposable
{
}
// This generic class can be call who is struct such as integer.
public class StructGenericClass<T> where T : struct
{
}
// This generic class can be call who is struct such as integer.
public class ClassGenericClass<T> where T : class ,new()
{
}
class Program
{
static void Main(string[] args)
{
// We know DataTable implements from IDisposable
DisposableGenericClass<DataTable> disposableObj = new DisposableGenericClass<DataTable>();
//int,decimal is value type which is struct
StructGenericClass<decimal> structObj = new StructGenericClass<decimal>();
//Program is parameterless constructor
ClassGenericClass<Program> classObj = new ClassGenericClass<Program>();
Console.ReadLine();
}
}
Generic And Abstract Class:
- Example 01 :
{
public abstract T Add(T arg1, T arg2);
public abstract T Subtract(T arg1, T arg2);
}
public class Calculator : BaseCalculator<int>
{
public override int Add(int arg1, int arg2)
{
return arg1 + arg2;
}
public override int Subtract(int arg1, int arg2)
{
return arg1 - arg2;
}
}
class Program
{
static void Main(string[] args)
{
Calculator obj = new Calculator();
int addValue=obj.Add(5, 4);
Console.WriteLine("5+4=" + addValue);
int subValue = obj.Subtract(5, 4);
Console.WriteLine("5-4=" + subValue);
}
}
Output: 5+4=9
5-4=1
Generic And Interface:
- Example 01 :
{
T Add(T arg1, T arg2);
T Subtract(T arg1, T arg2);
}
public class Calculator : IBaseCalculator<int>
{
public int Add(int arg1, int arg2)
{
return arg1 + arg2;
}
public int Subtract(int arg1, int arg2)
{
return arg1 - arg2;
}
}
class Program
{
static void Main(string[] args)
{
Calculator obj = new Calculator();
int addValue=obj.Add(5, 4);
Console.WriteLine("5+4=" + addValue);
int subValue = obj.Subtract(5, 4);
Console.WriteLine("5-4=" + subValue);
}
}
Output: 5+4=9
5-4=1
Generic Methods:
{
static void Swap<T>(ref T tFirstParam, ref T tSecondParam)
{
T tempValue;
tempValue = tFirstParam;
tFirstParam = tSecondParam;
tSecondParam = tempValue;
}
static void Main(string[] args)
{
int firstValue = 10;
int secondValue = 20;
Console.WriteLine("Before swap : firstValue={0} and secondValue={1}", firstValue, secondValue);
Swap<int>(ref firstValue,ref secondValue);
Console.WriteLine("After swap : firstValue={0} and secondValue={1}", firstValue, secondValue);
string firstName = "Sani";
string lastName = "Talukder";
Console.WriteLine("Before swap : firstName={0} and lastName={1}", firstName, lastName);
Swap<string>(ref firstName, ref lastName);
Console.WriteLine("After swap : firstName={0} and lastName={1}", firstName, lastName);
Console.ReadLine();
}
}
5-4=1
Generic Methods:
- Example 01 :
{
static void Swap<T>(ref T tFirstParam, ref T tSecondParam)
{
T tempValue;
tempValue = tFirstParam;
tFirstParam = tSecondParam;
tSecondParam = tempValue;
}
static void Main(string[] args)
{
int firstValue = 10;
int secondValue = 20;
Console.WriteLine("Before swap : firstValue={0} and secondValue={1}", firstValue, secondValue);
Swap<int>(ref firstValue,ref secondValue);
Console.WriteLine("After swap : firstValue={0} and secondValue={1}", firstValue, secondValue);
string firstName = "Sani";
string lastName = "Talukder";
Console.WriteLine("Before swap : firstName={0} and lastName={1}", firstName, lastName);
Swap<string>(ref firstName, ref lastName);
Console.WriteLine("After swap : firstName={0} and lastName={1}", firstName, lastName);
Console.ReadLine();
}
}
