C#提供5种泛型:类、结构、接口、委托和方法。在泛型类声明中,字符串由尖括号和T构成(也可以是其他标识符),在类声明的主体中,每一个T都会被编译器替换为实际类型。T被称为类型参数(type arguments),泛型类形如:

 

1 class MyGenericClass<t S>
2 {
3 //...
4  }</t>

 

1,下例是如果定义以及实现一个泛型类

 

代码
1 using System;
2  using System.Collections;
3  using System.Text;
4
5  namespace GenernicsDemo
6 {
7
8 class Person<T, S> // 泛型类
9   {
10 /// <summary>
11 /// 1 类的定义: T,S 作为属性,域的类型或者方法的参数类型
12 /// 2 类的实例化:Person<string, int> me = new Person<string, int>("Wang Hongjian", 23); 或者Person<int, int> me2 = new Person<int, int>(1, 2);
13 /// </summary>
14   private T name;
15 private S age;
16
17 public Person(T name, S age) /*构造*/
18 {
19 this.name = name;
20 this.age = age;
21 }
22
23 public void Display<T>(T[] names) /*泛型方法*/
24 {
25 ///<summary>
26 ///1 定义:
27 ///2 实现: 实例名.Display<string>[string数组名]
28 ///</summary>
29   Console.WriteLine("Name is {0},and the age is {1}.", name, age);
30 foreach (T _name in names)
31 {
32 Console.WriteLine("names are {0}", _name);
33 }
34 }
35 }
36
37 class Program
38 {
39 static void Main(string[] args)
40 {
41 Person<string, int> me = new Person<string, int>("Wang Hongjian", 23);
42 string[] hobbies = new string[] { "music", "reading", "sports" };
43 double[] moneys = new double[] { 12.34, 15.24, 78.34, 34.44 };
44
45 me.Display<string>(hobbies);
46 me.Display<double>(moneys);
47
48 Person<int, int> me2 = new Person<int, int>(1, 2);
49 me2.Display<string>(hobbies);
50 Console.ReadKey();
51 }
52 }
53 }
54  

 

2,下面是泛型接口的实现

 

代码

using System;
using System.Collections.Generic;
using System.Text;

namespace GenericInterface
{
#region A generic interface
public interface IBinaryOperations<T> where T : struct
{
T Add(T arg1, T arg2);
T Subtract(T arg1, T arg2);
T Multiply(T arg1, T arg2);
T Divide(T arg1, T arg2);
}
#endregion

#region Implementation of generic interface.
public class BasicMath : IBinaryOperations<float>
{
public float Add(float arg1, float arg2)
{
return arg1 + arg2; }

public float Subtract(float arg1, float arg2)
{
return arg1 - arg2; }

public float Multiply(float arg1, float arg2)
{
return arg1 * arg2; }

public float Divide(float arg1, float arg2)
{
return arg1 / arg2; }
}
#endregion

class Program
{
static void Main(string[] args)
{
Console.WriteLine(
"***** Generic Interfaces *****\n");
BasicMath m
= new BasicMath();
Console.WriteLine(
"1.98 + 1.3 = {0}", m.Add(1.98F, 1.3F));
Console.ReadLine();
}
}
}

 

3,IEnumerable <T>的列子(阅读前请参阅xelement的文章)

 

代码
1 using System;
2  using System.Collections.Generic;
3  using System.Linq;
4  using System.Text;
5  using System.Xml.Linq;
6  using System.Collections;
7  namespace Indexer
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 XElement xmlTree = new XElement("Root",
14 new XElement("Child1", 1),
15 new XElement("Child2", 8),
16 new XElement("Child3", 3),
17 new XElement("Child4", 4),
18 new XElement("Child5", 5)
19 );
20
21 Console.WriteLine(xmlTree);
22
23 XElement xmlTreet = new XElement("Root",
24 from elt in xmlTree.Elements()
25 where ((int)elt >= 3 && (int)elt <= 5)
26 select elt);
27 //foreach (XElement el in xmlTree2)
28   Console.WriteLine(xmlTreet);
29
30 IEnumerable<XElement> elements =
31 from el in xmlTree.Elements()
32 where (int)el <= 3
33 select el;
34 foreach (XElement el in elements)
35 Console.WriteLine(el);
36
37 //the same, 因为xmlTree.Elements() 同样返回IEnumerable<XElement>
38   var a =
39 from el in xmlTree.Elements()
40 where (int)el <= 3
41 select el;
42 foreach (XElement el in a)
43 Console.WriteLine(el);
44
45 }
46 }
47 }
48
49