泛型介面 (C# 程式設計手冊)

定義泛型集合類別的介面或定義代表集合中項目的泛型類別通常很有用。 若要避免在實值型別上進行裝箱(boxing)和拆箱(unboxing)作業,最好是在泛型類別中使用泛型介面,例如IComparable<T>。 .NET 類別庫會定義數個泛型介面,以便與 命名空間中的 System.Collections.Generic 集合類別搭配使用。 如需這些介面的詳細資訊,請參閱 泛型介面。

當介面指定為類型參數的條件約束時,只能使用實作介面的類型。 下列程式代碼範例顯示 SortedList<T> 類別衍生自 GenericList<T> 類別。 如需詳細資訊,請參閱 泛型簡介。 SortedList<T> 加入條件約束 where T : IComparable<T>。 此條件約束可讓 BubbleSort 中的 SortedList<T> 方法在清單元素上使用泛型 CompareTo 方法。 在此範例中,list 元素是實作 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屬性 getter 中,使用object來取代GetEnumerator。 因為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 方法的介面都會宣告其中一個型別參數必須實作宣告的介面。 編譯程式接著會使用提供的型別自變數來解析宣告成員的類型。

另請參閱