Basically, generic is used when you need to write a class or a function which can work with any kind of data types. You can define classes and functions with a place holder. You can use it something like this:
using System;
// < > to specify Parameter type
public class example <T> {
// private data members
private T data;
// using properties
public T value
{
// using accessors
get
{
return this.data;
}
set
{
this.data = value;
}
}
}
// Driver class
class Test {
// Main method
static void Main(string[] args)
{
// instance of string type
example<string> name = new example1<string>();
name.value = "anything";
// instance of float type
example<float> version = new example<float>();
version.value = 5.0F;
Console.WriteLine(name.value);
// display 5
Console.WriteLine(version.value);
}
}