泛型允许延迟编程元素的类或方法的数据类型的规范,直到它在程序中实际使用时确定。 换句话说,泛型允许编写一个可以使用任何数据类型的类或方法。
为类或方法编写规范,使用数据类型的替代参数。当编译器遇到类的构造函数或方法的函数调用时,它会生成代码来处理特定的数据类型。看看下面一个简单的例子将有助于理解这个概念:
using system;
using system.collections.generic;
namespace genericapplication
{
public class mygenericarray<t>
{
private t[] array;
public mygenericarray(int size)
{
array = new t[size + 1];
}
public t getitem(int index)
{
return array[index];
}
public void setitem(int index, t value)
{
array[index] = value;
}
}
class tester
{
static void main(string[] args)
{
//declaring an int array
mygenericarray<int> intarray = new mygenericarray<int>(5);
//setting values
for (int i = 0; i < 5; i++)
{
intarray.setitem(i, i * 10);
}
//retrieving the values
for (int i = 0; i < 5; i++)
{
console.write(intarray.getitem(i) + " ");
}
console.writeline();
//declaring a character array
mygenericarray<char> chararray = new mygenericarray<char>(5);
//setting values
for (int i = 0; i < 5; i++)
{
chararray.setitem(i, (char)(i + 97));
}
//retrieving the values
for (int c = 0; c < 5; c++)
{
console.write(chararray.getitem(c) + " ");
}
console.writeline();
console.readkey();
}
}
}
当上述代码被编译并执行时,它产生以下结果:
0 10 20 30 40
a b c d e
泛型是一种通过以下方式丰富程序的技术:
.net framework
类库在system.collections.generic
命名空间中包含几个新的通用集合类。开发者使用这些通用集合类,而不是system.collections
命名空间中的集合类。在前面的例子中,使用了一个泛型类; 可以声明一个类型参数的泛型方法。以下程序说明了以下概念:
using system;
using system.collections.generic;
namespace genericmethodappl
{
class program
{
static void swap<t>(ref t lhs, ref t rhs)
{
t temp;
temp = lhs;
lhs = rhs;
rhs = temp;
}
static void main(string[] args)
{
int a, b;
char c, d;
a = 100;
b = 201;
c = 'y';
d = 'b';
//display values before swap:
console.writeline("int values before calling swap:");
console.writeline("a = {0}, b = {1}", a, b);
console.writeline("char values before calling swap:");
console.writeline("c = {0}, d = {1}", c, d);
//call swap
swap<int>(ref a, ref b);
swap<char>(ref c, ref d);
//display values after swap:
console.writeline("int values after calling swap:");
console.writeline("a = {0}, b = {1}", a, b);
console.writeline("char values after calling swap:");
console.writeline("c = {0}, d = {1}", c, d);
console.readkey();
}
}
}
当上述代码被编译并执行时,它产生以下结果:
int values before calling swap:
a = 100, b = 201
char values before calling swap:
c = y, d = b
int values after calling swap:
a = 201, b = 100
char values after calling swap:
c = b, d = y
可以使用类型参数定义一个泛型委托。 例如:
delegate t numberchanger<t>(t n);
以下示例显示了如何使用此委托:
using system;
using system.collections.generic;
delegate t numberchanger<t>(t n);
namespace genericdelegateappl
{
class testdelegate
{
static int num = 101;
public static int addnum(int p)
{
num += p;
return num;
}
public static int multnum(int q)
{
num *= q;
return num;
}
public static int getnum()
{
return num;
}
static void main(string[] args)
{
//create delegate instances
numberchanger<int> nc1 = new numberchanger<int>(addnum);
numberchanger<int> nc2 = new numberchanger<int>(multnum);
//calling the methods using the delegate objects
nc1(25);
console.writeline("value of num: {0}", getnum());
nc2(5);
console.writeline("value of num: {0}", getnum());
console.readkey();
}
}
}
value of num: 126
value of num: 630