Generic class example.
public class TestGeneric<T>
{
T objectname;
public simpleGeneric(T obj)
{
objectname = obj;
}
public T getobj()
{
return objectname;
}
public void display()
{
Console.WriteLine("The type is:" + typeof(T));
}
}we have created a TestGeneric class with generic type.
class Program
{
static void Main(string[] args)
{
//Create object with int type
TestGeneric<int> Gen = new TestGeneric<int>(107);
Gen.display();
int i = Gen.getobj();
Console.WriteLine("The Value of i:{0}", i);
//Create object with string type
TestGeneric<string> GenString = new TestGeneric<string>("Welcome to Generic");
GenString.display();
string str = GenString.getobj();
Console.WriteLine("The Value of str:{0}", str);
Console.ReadLine();
}
}call with generic class with diffenet datatype.
OutPut :
The type is:System.Int32
The Value of i:107
The type is:System.String
The Value of str:Welcome to Generic