成人免费xxxxx在线视频软件_久久精品久久久_亚洲国产精品久久久_天天色天天色_亚洲人成一区_欧美一级欧美三级在线观看

C# 泛型接口應用淺析

開發 后端
C# 泛型接口的概念是什么?C# 泛型接口的使用是如何進行的呢?那么本文就向你詳細C# 泛型接口的內容。

C# 泛型接口的實用性:為泛型集合類或表示集合中項的泛型類定義接口通常很有用。對于泛型類,使用泛型接口十分可取,例如使用 IComparable﹤T﹥ 而不使用 IComparable,這樣可以避免值類型的裝箱和取消裝箱操作。.NET Framework 2.0 類庫定義了若干新的泛型接口,以用于 System.Collections.Generic 命名空間中新的集合類。將接口指定為類型參數的約束時,只能使用實現此接口的類型。下面的代碼示例顯示從 GenericList﹤T﹥ 類派生的 SortedList﹤T﹥ 類。SortedList﹤T﹥ 添加了約束 where T : IComparable﹤T﹥。這將使 SortedList﹤T﹥ 中的 BubbleSort 方法能夠對列表元素使用泛型 CompareTo 方法。在此示例中,列表元素為簡單類,即實現 IComparable﹤Person﹥ 的 Person。

C# 泛型接口代碼

  1. //Type parameter T in angle brackets.  
  2. public class GenericList﹤T﹥ :  
  3.  System.Collections.Generic.IEnumerable﹤T﹥  
  4. {  
  5. protected Node head;  
  6. protected Node current = null;  
  7.  
  8. // Nested class is also generic on T  
  9. protected class Node  
  10. {  
  11. public Node next;  
  12. private T data;  //T as private member datatype  
  13.  
  14. public Node(T t)  //T used in non-generic constructor  
  15. {  
  16. next = null;  
  17. data = t;  
  18. }  
  19.  
  20. public Node Next  
  21. {  
  22. get { return next; }  
  23. set { next = value; }  
  24. }  
  25.  
  26. public T Data  //T as return type of property  
  27. {  
  28. get { return data; }  
  29. set { data = value; }  
  30. }  
  31. }  
  32.  
  33. public GenericList()  //constructor  
  34. {  
  35. head = null;  
  36. }  
  37.  
  38. public void AddHead(T t)  //T as method parameter type  
  39. {  
  40. Node n = new Node(t);  
  41. n.Next = head;  
  42. head = n;  
  43. }  
  44.  
  45. // Implementation of the iterator  
  46. public System.Collections.Generic.IEnumerator﹤T﹥ GetEnumerator()  
  47. {  
  48. Node current = head;  
  49. while (current != null)  
  50. {  
  51. yield return current.Data;  
  52. current = current.Next;  
  53. }  
  54. }  
  55.  
  56. // IEnumerable﹤T﹥ inherits from IEnumerable, therefore this class   
  57. // must implement both the generic and non-generic versions of   
  58. // GetEnumerator. In most cases, the non-generic method can   
  59. // simply call the generic method.  
  60. System.Collections.IEnumerator   
  61. System.Collections.IEnumerable.GetEnumerator()  
  62. {  
  63. return GetEnumerator();  
  64. }  
  65. }  
  66.  
  67. public class SortedList﹤T﹥ :  
  68.  GenericList﹤T﹥ where T : System.IComparable﹤T﹥  
  69. {  
  70. // A simple, unoptimized sort algorithm that   
  71. // orders list elements from lowest to highest:  
  72.  
  73. public void BubbleSort()  
  74. {  
  75. if (null == head || null == head.Next)  
  76. {  
  77. return;  
  78. }  
  79. bool swapped;  
  80.  
  81. do 
  82. {  
  83. Node previous = null;  
  84. Node current = head;  
  85. swapped = false;  
  86.  
  87. while (current.next != null)  
  88. {  
  89. //  Because we need to call this method, the SortedList  
  90. //  class is constrained on IEnumerable﹤T﹥  
  91. if (current.Data.CompareTo(current.next.Data) ﹥ 0)  
  92. {  
  93. Node tmp = current.next;  
  94. current.next = current.next.next;  
  95. tmp.next = current;  
  96.  
  97. if (previous == null)  
  98. {  
  99. head = tmp;  
  100. }  
  101. else 
  102. {  
  103. previous.next = tmp;  
  104. }  
  105. previous = tmp;  
  106. swapped = true;  
  107. }  
  108. else 
  109. {  
  110. previous = current;  
  111. current = current.next;  
  112. }  
  113. }  
  114. while (swapped);  
  115. }  
  116. }  
  117.  
  118. // A simple class that implements   
  119. //IComparable﹤T﹥ using itself as the   
  120. // type argument. This is a common  
  121. // design pattern in objects that   
  122. // are stored in generic lists.  
  123. public class Person : System.IComparable﹤Person﹥  
  124. {  
  125. string name;  
  126. int age;  
  127.  
  128. public Person(string s, int i)  
  129. {  
  130. name = s;  
  131. age = i;  
  132. }  
  133.  
  134. // This will cause list elements  
  135. // to be sorted on age values.  
  136. public int CompareTo(Person p)  
  137. {  
  138. return age - p.age;  
  139. }  
  140.  
  141. public override string ToString()  
  142. {  
  143. return name + ":" + age;  
  144. }  
  145.  
  146. // Must implement Equals.  
  147. public bool Equals(Person p)  
  148. {  
  149. return (this.age == p.age);  
  150. }  
  151. }  
  152.  
  153. class Program  
  154. {  
  155. static void Main()  
  156. {  
  157. //Declare and instantiate a new generic SortedList class.  
  158. //Person is the type argument.  
  159. SortedList﹤Person﹥ list = new SortedList﹤Person﹥();  
  160.  
  161. //Create name and age values to initialize Person objects.  
  162. string[] names = new string[]   
  163. {   
  164. "Franscoise",   
  165. "Bill",   
  166. "Li",   
  167. "Sandra",   
  168. "Gunnar",   
  169. "Alok",   
  170. "Hiroyuki",   
  171. "Maria",   
  172. "Alessandro",   
  173. "Raul"   
  174. };  
  175.  
  176. int[] ages = new int[] { 45, 19, 28,  
  177.  23, 18, 9, 108, 72, 30, 35 };  
  178.  
  179. //Populate the list.  
  180. for (int x = 0; x ﹤ 10; x++)  
  181. {  
  182. list.AddHead(new Person(names[x], ages[x]));  
  183. }  
  184.  
  185. //Print out unsorted list.  
  186. foreach (Person p in list)  
  187. {  
  188. System.Console.WriteLine(p.ToString());  
  189. }  
  190. System.Console.WriteLine("Done with unsorted list");  
  191.  
  192. //Sort the list.  
  193. list.BubbleSort();  
  194.  
  195. //Print out sorted list.  
  196. foreach (Person p in list)  
  197. {  
  198. System.Console.WriteLine(p.ToString());  
  199. }  
  200. System.Console.WriteLine("Done with sorted list");  
  201. }  

可將多重接口指定為單個類型上的約束,如下所示:

C# 泛型接口代碼

  1. class Stack﹤T﹥ where T : System.IComparable﹤T﹥, IEnumerable﹤T﹥  
  2. {  

一個接口可定義多個類型參數,如下所示:

C# 泛型接口代碼

  1. interface IDictionary﹤K, V﹥  
  2. {  

類之間的繼承規則同樣適用于接口:

C# 泛型接口代碼

  1. interface IMonth﹤T﹥ { }  
  2.  
  3. interface IJanuary : IMonth﹤int﹥ { }  //No error  
  4. interface IFebruary﹤T﹥ : IMonth﹤int﹥ { }  //No error  
  5. interface IMarch﹤T﹥: IMonth﹤T﹥ { }//No error  
  6. //interface IApril﹤T﹥  : IMonth﹤T, U﹥ {}  //Error 

如果泛型接口為逆變的,即僅使用其類型參數作為返回值,則此泛型接口可以從非泛型接口繼承。在 .NET Framework 類庫中,IEnumerable﹤T﹥ 從 IEnumerable 繼承,因為 IEnumerable﹤T﹥ 僅在 GetEnumerator 的返回值和當前屬性 getter 中使用 T。

具體類可以實現已關閉的構造接口,如下所示:

C# 泛型接口代碼

  1. interface IBaseInterface﹤T﹥ { }  
  2.  
  3. class SampleClass : IBaseInterface﹤string﹥ { } 

只要類參數列表提供了接口必需的所有參數,泛型類便可以實現泛型接口或已關閉的構造接口,如下所示:

C# 泛型接口代碼

  1. interface IBaseInterface1﹤T﹥ { }  
  2. interface IBaseInterface2﹤T, U﹥ { }  
  3.  
  4. class SampleClass1﹤T﹥ :   
  5. IBaseInterface1﹤T﹥ { }//No error  
  6. class SampleClass2﹤T﹥ :   
  7. IBaseInterface2﹤T, string﹥ { }//No error 

對于泛型類、泛型結構或泛型接口中的方法,控制方法重載的規則相同。

C# 泛型接口的相關內容就向你介紹到這里,希望對你了解和學習C# 泛型接口有所幫助。

【編輯推薦】

  1. C# 強制類型轉換與C# 泛型淺析
  2. C# 泛型類概念與實例的理解應用淺析
  3. C# 泛型的優點淺談
  4. C# 泛型類型參數淺析
  5. C# 類型參數約束分析及應用淺析
責任編輯:仲衡 來源: MSDN
相關推薦

2009-08-24 11:35:20

C# 泛型應用

2009-08-24 17:58:19

C# 泛型集合

2009-08-24 17:27:05

C#泛型應用

2009-08-24 16:39:19

C# 泛型應用

2009-08-24 17:39:21

C# 泛型集合

2009-08-24 14:51:25

C# 泛型泛型類型

2009-08-24 18:15:24

C# Dictiona

2009-08-24 15:50:23

C# 泛型C# 泛型委托

2009-08-24 14:20:13

C# 強制類型轉換

2009-08-24 14:26:42

C# 泛型類

2009-08-24 10:07:57

C#泛型處理

2009-08-07 08:53:52

C# ICloneab

2009-08-24 13:31:38

C# 泛型約束

2009-08-24 10:37:27

C# 泛型

2009-08-24 15:28:19

C# 泛型方法

2009-08-24 16:01:44

C# 泛型

2009-08-24 16:19:42

C# 泛型方法

2009-08-24 13:41:23

C# 泛型約束

2009-08-27 13:05:06

C#接口特點C#接口實例

2009-08-31 16:37:20

C#接口定義
點贊
收藏

51CTO技術棧公眾號

主站蜘蛛池模板: 国产亚洲精品精品国产亚洲综合 | 国产成人精品久久二区二区 | 精品免费在线 | 日本一区二区三区视频在线 | 亚洲国产成人精品一区二区 | 男女羞羞视频在线观看 | 国产三区视频在线观看 | 97精品超碰一区二区三区 | 久久久免费少妇高潮毛片 | 日本视频一区二区三区 | 日本精品一区二区三区在线观看视频 | 国产精品久久久久久 | 欧美大片久久久 | 精品无码久久久久久国产 | 欧美精 | 国产目拍亚洲精品99久久精品 | 亚州av在线| 看毛片网站 | 久久伊人精品 | 国产一级毛片视频 | 久久草视频 | 久久99精品久久久久久青青日本 | 精品国产一区二区三区性色av | 无码一区二区三区视频 | 国产精品1 | 九九精品视频在线 | 在线国产一区 | 国产精品永久在线观看 | 天天干天天爽 | 国产成人午夜电影网 | 人干人人 | 亚洲一二三区免费 | 中文福利视频 | 成人免费一区二区三区视频网站 | 日本一二三区在线观看 | 午夜三区 | 久久国产精品久久 | 国产一区二区三区四区 | 亚洲高清在线观看 | 欧美中文字幕一区二区 | 亚洲美女视频 |