为泛型集合类或表示集合中项的泛型类定义接口通常很有用。对于泛型类,使用泛型接口十分可取,例如使用 IComparable<T> 而不使用 IComparable,这样可以避免值类型的装箱和取消装箱操作。.NET Framework 2.0 类库定义了若干新的泛型接口,以用于 System.Collections.Generic 命名空间中新的集合类。
将接口指定为类型参数的约束时,只能使用实现此接口的类型。下面的代码示例显示从 GenericList<T> 类派生的 SortedList<T> 类。有关更多信息,请参见泛型介绍(C# 编程指南)。SortedList<T> 添加了约束 where T : IComparable<T>。这将使 SortedList<T> 中的 BubbleSort 方法能够对列表元素使用泛型 CompareTo 方法。在此示例中,列表元素为简单类,即实现 IComparable<Person> 的 Person。
//
Type parameter T in angle brackets.
public
class
GenericList
<
T
>
: System.Collections.Generic.IEnumerable
<
T
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
protected Node head;
protected Node current = null;
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
// Nested class is also generic on T
protected class Node
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
public Node next;
private T data; //T as private member datatype
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public Node(T t) //T used in non-generic constructor
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
next = null;
data = t;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public Node Next
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return next; }
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ next = value; }
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public T Data //T as return type of property
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
get
{ return data; }
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
set
{ data = value; }
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public GenericList() //constructor
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
head = null;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public void AddHead(T t) //T as method parameter type
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Node n = new Node(t);
n.Next = head;
head = n;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
// Implementation of the iterator
public System.Collections.Generic.IEnumerator<T> GetEnumerator()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Node current = head;
while (current != null)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
yield return current.Data;
current = current.Next;
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
// IEnumerable<T> inherits from IEnumerable, therefore this class
// must implement both the generic and non-generic versions of
// GetEnumerator. In most cases, the non-generic method can
// simply call the generic method.
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return GetEnumerator();
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
public
class
SortedList
<
T
>
: GenericList
<
T
>
where T : System.IComparable
<
T
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
// A simple, unoptimized sort algorithm that
// orders list elements from lowest to highest:
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public void BubbleSort()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
if (null == head || null == head.Next)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return;
}
bool swapped;
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
do
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Node previous = null;
Node current = head;
swapped = false;
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
while (current.next != null)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
// Because we need to call this method, the SortedList
// class is constrained on IEnumerable<T>
if (current.Data.CompareTo(current.next.Data) > 0)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
Node tmp = current.next;
current.next = current.next.next;
tmp.next = current;
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
if (previous == null)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
head = tmp;
}
else
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
previous.next = tmp;
}
previous = tmp;
swapped = true;
}
else
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
previous = current;
current = current.next;
}
}
} while (swapped);
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
//
A simple class that implements IComparable<T> using itself as the
//
type argument. This is a common design pattern in objects that
//
are stored in generic lists.
public
class
Person : System.IComparable
<
Person
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
string name;
int age;
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public Person(string s, int i)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
name = s;
age = i;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
// This will cause list elements to be sorted on age values.
public int CompareTo(Person p)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return age - p.age;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
public override string ToString()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return name + ":" + age;
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
// Must implement Equals.
public bool Equals(Person p)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
return (this.age == p.age);
}
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
class
Program
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
static void Main()
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
//Declare and instantiate a new generic SortedList class.
//Person is the type argument.
SortedList<Person> list = new SortedList<Person>();
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
//Create name and age values to initialize Person objects.
string[] names = new string[]
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
"Franscoise",
"Bill",
"Li",
"Sandra",
"Gunnar",
"Alok",
"Hiroyuki",
"Maria",
"Alessandro",
"Raul"
};
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
int[] ages = new int[]
{ 45, 19, 28, 23, 18, 9, 108, 72, 30, 35 };
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
//Populate the list.
for (int x = 0; x < 10; x++)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
list.AddHead(new Person(names[x], ages[x]));
}
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
//Print out unsorted list.
foreach (Person p in list)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.WriteLine(p.ToString());
}
System.Console.WriteLine("Done with unsorted list");
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
//Sort the list.
list.BubbleSort();
![](http://writeblog.csdn.net/Images/OutliningIndicators/InBlock.gif)
//Print out sorted list.
foreach (Person p in list)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
System.Console.WriteLine(p.ToString());
}
System.Console.WriteLine("Done with sorted list");
}
}
可将多重接口指定为单个类型上的约束,如下所示:
class
Stack
<
T
>
where T : System.IComparable
<
T
>
, IEnumerable
<
T
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
}
一个接口可定义多个类型参数,如下所示:
interface
IDictionary
<
K, V
>
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
}
类之间的继承规则同样适用于接口:
interface
IMonth
<
T
>
{ }
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
interface
IJanuary : IMonth
<
int
>
{ }
//
No error
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
interface
IFebruary
<
T
>
: IMonth
<
int
>
{ }
//
No error
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
interface
IMarch
<
T
>
: IMonth
<
T
>
{ }
//
No error
//
interface IApril<T> : IMonth<T, U> {}
//
Error
如果泛型接口为逆变的,即仅使用其类型参数作为返回值,则此泛型接口可以从非泛型接口继承。在 .NET Framework 类库中,IEnumerable<T> 从 IEnumerable 继承,因为 IEnumerable<T> 仅在 GetEnumerator 的返回值和当前属性 getter 中使用 T。
具体类可以实现已关闭的构造接口,如下所示:
interface
IBaseInterface
<
T
>
{ }
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
class
SampleClass : IBaseInterface
<
string
>
{ }
只要类参数列表提供了接口必需的所有参数,泛型类便可以实现泛型接口或已关闭的构造接口,如下所示:
interface
IBaseInterface1
<
T
>
{ }
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
interface
IBaseInterface2
<
T, U
>
{ }
![](http://writeblog.csdn.net/Images/OutliningIndicators/None.gif)
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
class
SampleClass1
<
T
>
: IBaseInterface1
<
T
>
{ }
//
No error
![](http://writeblog.csdn.net/Images/OutliningIndicators/ExpandedBlockStart.gif)
class
SampleClass2
<
T
>
: IBaseInterface2
<
T,
string
>
{ }
//
No error
对于泛型类、泛型结构或泛型接口中的方法,控制方法重载的规则相同。