泛型接口 (C# 编程指南)

为泛型集合类或表示集合中的项的泛型类定义接口通常很有用。 为了避免对值类型执行装箱和取消装箱操作,最好在泛型类上使用泛型接口,例如IComparable<T>。 .NET 类库定义了多个用于命名空间中的集合类的 System.Collections.Generic 泛型接口。 有关这些接口的详细信息,请参阅 泛型接口。

将接口指定为类型参数的约束时,只能使用实现接口的类型。 下面的代码示例显示一个 SortedList<T> 类派生自 GenericList<T> 类。 有关详细信息,请参阅 泛型简介。 SortedList<T> 添加约束 where T : IComparable<T>。 此约束使BubbleSort方法能够在SortedList<T>中对列表元素使用泛型CompareTo方法。 在此示例中,列表元素是一个简单类Person,该类实现了IComparable<Person>。

//Type parameter T in angle brackets.
public class GenericList<T> : System.Collections.Generic.IEnumerable<T>
{
    protected Node head;
    protected Node current = null;

    // Nested class is also generic on T
    protected class Node
    {
        public Node next;
        private T data;  //T as private member datatype

        public Node(T t)  //T used in non-generic constructor
        {
            next = null;
            data = t;
        }

        public Node Next
        {
            get { return next; }
            set { next = value; }
        }

        public T Data  //T as return type of property
        {
            get { return data; }
            set { data = value; }
        }
    }

    public GenericList()  //constructor
    {
        head = null;
    }

    public void AddHead(T t)  //T as method parameter type
    {
        Node n = new Node(t);
        n.Next = head;
        head = n;
    }

    // Implementation of the iterator
    public System.Collections.Generic.IEnumerator<T> GetEnumerator()
    {
        Node current = head;
        while (current != null)
        {
            yield return current.Data;
            current = current.Next;
        }
    }

    // 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()
    {
        return GetEnumerator();
    }
}

public class SortedList<T> : GenericList<T> where T : System.IComparable<T>
{
    // A simple, unoptimized sort algorithm that
    // orders list elements from lowest to highest:

    public void BubbleSort()
    {
        if (null == head || null == head.Next)
        {
            return;
        }
        bool swapped;

        do
        {
            Node previous = null;
            Node current = head;
            swapped = false;

            while (current.next != null)
            {
                //  Because we need to call this method, the SortedList
                //  class is constrained on IComparable<T>
                if (current.Data.CompareTo(current.next.Data) > 0)
                {
                    Node tmp = current.next;
                    current.next = current.next.next;
                    tmp.next = current;

                    if (previous == null)
                    {
                        head = tmp;
                    }
                    else
                    {
                        previous.next = tmp;
                    }
                    previous = tmp;
                    swapped = true;
                }
                else
                {
                    previous = current;
                    current = current.next;
                }
            }
        } while (swapped);
    }
}

// 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>
{
    string name;
    int age;

    public Person(string s, int i)
    {
        name = s;
        age = i;
    }

    // This will cause list elements to be sorted on age values.
    public int CompareTo(Person p)
    {
        return age - p.age;
    }

    public override string ToString()
    {
        return name + ":" + age;
    }

    // Must implement Equals.
    public bool Equals(Person p)
    {
        return (this.age == p.age);
    }
}

public class Program
{
    public static void Main()
    {
        //Declare and instantiate a new generic SortedList class.
        //Person is the type argument.
        SortedList<Person> list = new SortedList<Person>();

        //Create name and age values to initialize Person objects.
        string[] names =
        [
            "Franscoise",
            "Bill",
            "Li",
            "Sandra",
            "Gunnar",
            "Alok",
            "Hiroyuki",
            "Maria",
            "Alessandro",
            "Raul"
        ];

        int[] ages = [45, 19, 28, 23, 18, 9, 108, 72, 30, 35];

        //Populate the list.
        for (int x = 0; x < 10; x++)
        {
            list.AddHead(new Person(names[x], ages[x]));
        }

        //Print out unsorted list.
        foreach (Person p in list)
        {
            System.Console.WriteLine(p.ToString());
        }
        System.Console.WriteLine("Done with unsorted list");

        //Sort the list.
        list.BubbleSort();

        //Print out sorted list.
        foreach (Person p in list)
        {
            System.Console.WriteLine(p.ToString());
        }
        System.Console.WriteLine("Done with sorted list");
    }
}

可以将多个接口指定为单个类型的约束,如下所示:

class Stack<T> where T : System.IComparable<T>, IEnumerable<T>
{
}

接口可以定义多个类型参数,如下所示:

interface IDictionary<K, V>
{
}

适用于类的继承规则也适用于接口:

interface IMonth<T> { }

interface IJanuary : IMonth<int> { }  //No error
interface IFebruary<T> : IMonth<int> { }  //No error
interface IMarch<T> : IMonth<T> { }    //No error
                                       //interface IApril<T>  : IMonth<T, U> {}  //Error

泛型接口可以从非泛型接口继承。 在 .NET 类库中, IEnumerable<T> 继承自 IEnumerable. 当泛型接口继承自非泛型接口时,类型参数通常会在重写的成员中替换 object 。 例如,IEnumerable<T> 在 T 的返回值和 object 的属性 getter 中使用 GetEnumerator 代替 Current。 由于 T 仅在这些成员的输出位置中使用, IEnumerable<T> 因此可以标记为协变。 如果在重写成员中将 T 用作输入位置,接口就无法协变,编译器将会生成错误。

具体类可以实现封闭式构造接口,如下所示:

interface IBaseInterface<T> { }

class SampleClass : IBaseInterface<string> { }

只要类参数列表提供接口所需的所有参数,泛型类就可以实现泛型接口或封闭构造接口,如下所示:

interface IBaseInterface1<T> { }
interface IBaseInterface2<T, U> { }

class SampleClass1<T> : IBaseInterface1<T> { }          //No error
class SampleClass2<T> : IBaseInterface2<T, string> { }  //No error

对于泛型类、泛型结构或泛型接口中的方法,控制方法重载的规则是相同的。 有关详细信息,请参阅 泛型方法。

接口可以声明 static abstract 或 static virtual 成员。 声明任一接口 static abstract 或 static virtual 成员的接口几乎始终是泛型接口。 编译器必须在编译时解析对 static virtual 和 static abstract 方法的调用。 接口中声明的 static virtual 和 static abstract 方法没有类似于类中声明的 virtual 或 abstract 方法的运行时调度机制。 相反,编译器使用编译时可用的类型信息。 这些成员通常在泛型接口中声明。 此外,声明 static virtual 或 static abstract 方法的大多数接口都声明了其中一个类型参数必须实现已声明的接口。 然后,编译器使用提供的类型参数解析声明成员的类型。

另请参阅