using System;
namespace demoapp
{
//We use < > to specify Parameter type publicclassGFG<T>
{//private data members private T data;
//using properties public T value
{
/using accessors
get
{
returnthis.data;
}
set
{
this.data = value;
}
}
}
//vehicle class classVehicle
{//Main method staticvoidMain(string[] args){
//instance of string type
GFG<string> company = new GFG<string>();
company.value = "Tata motors";
//instance of float type
GFG<float> version = new GFG<float>();
version.value = 6.0F;
//display Tata motors
Console.WriteLine(company.value);
//display 6
Console.WriteLine(version.value);
}
}
}
C# 中的泛型:
使用泛型,我们可以创建集合类。最好使用
System.Collections.Generic 命名空间而不是 System.Collections 命名空间中的类(例如 ArrayList)来创建泛型集合。泛型鼓励使用参数化类型,如下例所示: