<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 泛型接口(C# 編程指南) 為泛型集合類或表示集合中項的泛型類定義接口通常很有用。對于泛型類,使用泛型接口十分可取,例如使用 [IComparable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/4d7sx9hd.aspx) 而不使用 [IComparable](https://msdn.microsoft.com/zh-cn/library/system.icomparable.aspx),這樣可以避免值類型的裝箱和取消裝箱操作。.NET Framework 類庫定義了若干泛型接口,以用于 [System.Collections.Generic](https://msdn.microsoft.com/zh-cn/library/system.collections.generic.aspx) 命名空間中的集合類。 將接口指定為類型參數的約束時,只能使用實現此接口的類型。下面的代碼示例顯示從 SortedList&lt;T&gt; 類派生的 GenericList&lt;T&gt; 類。有關更多信息,請參見 [泛型介紹(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/0x6a29h6.aspx)。 SortedList&lt;T&gt; 添加約束 where T : IComparable&lt;T&gt;。這將使 SortedList&lt;T&gt; 中的 **BubbleSort** 方法能夠對列表元素使用泛型 [CompareTo](https://msdn.microsoft.com/zh-cn/library/43hc6wht.aspx) 方法。在此示例中,列表元素為簡單類,即實現 Person 的 IComparable&lt;Person&gt;。 ``` //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 IEnumerable<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); } } class Program { 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 = new string[] { "Franscoise", "Bill", "Li", "Sandra", "Gunnar", "Alok", "Hiroyuki", "Maria", "Alessandro", "Raul" }; int[] ages = new int[] { 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 Framework 類庫中,[IEnumerable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/9eekhta0.aspx) 從 [IEnumerable](https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerable.aspx) 繼承,因為 [IEnumerable&lt;T&gt;](https://msdn.microsoft.com/zh-cn/library/9eekhta0.aspx) 只在 [GetEnumerator](https://msdn.microsoft.com/zh-cn/library/s793z9y2.aspx) 的返回值和 [Current](https://msdn.microsoft.com/zh-cn/library/58e146b7.aspx) 屬性 getter 中使用 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 ``` 對于泛型類、泛型結構或泛型接口中的方法,控制方法重載的規則相同。有關更多信息,請參見 [泛型方法(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/twcad0zb.aspx)。 ## 請參閱 [C# 編程指南](https://msdn.microsoft.com/zh-cn/library/67ef8sbd.aspx) [泛型介紹(C# 編程指南)](https://msdn.microsoft.com/zh-cn/library/0x6a29h6.aspx) [接口(C# 參考)](https://msdn.microsoft.com/zh-cn/library/87d83y5b.aspx) [.NET Framework 中的泛型](https://msdn.microsoft.com/zh-cn/library/ms172192.aspx)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看