偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C# 泛型集合實例應(yīng)用淺析

開發(fā) 后端
C# 泛型集合實例應(yīng)用不僅向你介紹了C# 泛型集合的概念,還向你介紹了C# 泛型集合實例應(yīng)用的具體的事宜,希望對你學(xué)習(xí)C# 泛型集合有所幫助。

C# 泛型集合了解之前我們明白集合是OOP中的一個重要概念,C#中對集合的全面支持更是該語言的精華之一。C# 泛型是C# 2.0中的新增元素(C++中稱為模板),主要用于解決一系列類似的問題。這種機制允許將類名作為參數(shù)傳遞給泛型類型,并生成相應(yīng)的對象。將泛型(包括類、接口、方法、委托等)看作模板可能更好理解,模板中的變體部分將被作為參數(shù)傳進來的類名稱所代替,從而得到一個新的類型定義。泛型是一個比較大的話題,在此不作詳細解析,有興趣者可以查閱相關(guān)資料。

C# 泛型集合類用起來十分的方便快捷。在這篇隨筆里面,我將用鏈表來模擬c#中的 List﹤T﹥ 類的行為,廢話不多說,下面來看我的實現(xiàn)代碼,代碼中已經(jīng)寫了注釋,所以不再對代碼進行額外的說明:

  1. using System.Collections;  
  2.  
  3. class MyList﹤T﹥  
  4. {  
  5. private MyListNode firstNode;//首節(jié)點  
  6. private int count;//C# 泛型集合-節(jié)點計數(shù)  
  7.    
  8. public MyList()  
  9. {  
  10. this.firstNode = null;  
  11. this.count = 0;  
  12. }  
  13. //C# 泛型集合-得到List長度  
  14. public int GetLength()  
  15. {  
  16. return this.count;  
  17. }  
  18.  
  19. //增加一個節(jié)點  
  20. public void AddElement(T data)  
  21. {  
  22. MyListNode first = this.firstNode;  
  23. if(first==null)  
  24. {  
  25. this.firstNode=new MyListNode(data);  
  26. this.count++;  
  27. return;  
  28. }  
  29. while (first.next != null)  
  30. {  
  31. first = first.next;  
  32. }  
  33. first.next = new MyListNode(data);  
  34. this.count++;  
  35. }  
  36.  
  37. //C# 泛型集合-刪除一個節(jié)點  
  38. public bool Remove(T data)  
  39. {  
  40. MyListNode first = this.firstNode;  
  41. if (first.data.Equals(data))  
  42. {  
  43. this.firstNode = first.next;  
  44. this.count--;  
  45. return true;  
  46. }  
  47. while (first.next!=null)  
  48. {  
  49. if (first.next.data.Equals(data))  
  50. {  
  51. first.next = first.next.next;  
  52. this.count--;  
  53. return true;  
  54. }  
  55. }  
  56. return false;  
  57. }  
  58.  
  59. //C# 泛型集合-得到指定索引上的集合元素  
  60. public T GetAtIndex(int index)  
  61. {  
  62. int innercount = 1;  
  63. MyListNode first = this.firstNode;  
  64. if (index ﹥ count)  
  65. {  
  66. throw new Exception("Index out of boundary");  
  67. }  
  68. else 
  69. {  
  70. while (innercount ﹤ index)  
  71. {  
  72. first = first.next;  
  73. innercount++;  
  74. }  
  75. return first.data;  
  76. }  
  77. }  
  78.  
  79. //在指定的索引上插入新的元素  
  80. public void InsertAtIndex(int index,T data)  
  81. {  
  82. int innercount = 1;  
  83. MyListNode first = this.firstNode;  
  84. if (index ﹥ count)  
  85. {  
  86. throw new Exception("Index out of boundary");  
  87. }  
  88. if (index == 1)  
  89. {  
  90. this.firstNode = new MyListNode(data);  
  91. this.firstNode.next = first;  
  92. }  
  93. else 
  94. {  
  95. while (innercount ﹤ index - 1)  
  96. {  
  97. first = first.next;  
  98. innercount++;  
  99. }  
  100. MyListNode newNode = new MyListNode(data);  
  101. newNode.next = first.next;  
  102. first.next = newNode;  
  103. }  
  104. this.count++;  
  105. }  
  106.  
  107. //C# 泛型集合-刪除指定索引上的集合元素  
  108. public void RemoveAtIndex(int index)  
  109. {  
  110. int innercount = 1;  
  111. MyListNode first = this.firstNode;  
  112. if (index ﹥ count)  
  113. {  
  114. throw new Exception("Index out of boundary");  
  115. }  
  116. if (index == 1)  
  117. {  
  118. this.firstNode = first.next;  
  119. }  
  120. else 
  121. {  
  122. while (innercount ﹤ index - 1)  
  123. {  
  124. first = first.next;  
  125. innercount++;  
  126. }  
  127. first.next = first.next.next;  
  128. }  
  129. this.count--;  
  130. }  
  131.  
  132. //C# 泛型集合-刪除集合中的所有元素  
  133. public void RemoveAll()  
  134. {  
  135. this.firstNode = null;  
  136. this.count = 0;  
  137. }  
  138.  
  139. //為實現(xiàn)該集合類能用foreach進行遍歷  
  140. public IEnumerator GetEnumerator()  
  141. {  
  142. MyListNode first = this.firstNode;  
  143. while (first!= null)  
  144. {  
  145. yield return first.data;  
  146. first = first.next;  
  147. }  
  148. }  
  149.  
  150. //內(nèi)部節(jié)點類  
  151. private class MyListNode  
  152. {  
  153. public T data { getset; }//節(jié)點上的元素值  
  154. public MyListNode next { getset; }//節(jié)點的下一個節(jié)點  
  155. public MyListNode(T nodeData)  
  156. {  
  157. this.data = nodeData;  
  158. this.next = null;  
  159. }  
  160. }  
  161. }  

下面是C# 泛型集合對這個模擬類的使用:

  1. class Program  
  2. {  
  3. static void Main(string[] args)  
  4. {  
  5. MyList﹤string﹥ ml = new MyList﹤string﹥();  
  6. ml.AddElement("xu");  
  7. ml.AddElement("jin");  
  8. ml.AddElement("lin");  
  9. ml.AddElement("love");  
  10. ml.AddElement("jasmine");  
  11. ml.InsertAtIndex(4, "fiercely");  
  12. ml.RemoveAtIndex(2);  
  13. ml.Remove("lin");  
  14. foreach (string s in ml)  
  15. {  
  16. Console.WriteLine(s);  
  17. }  
  18. }  

C# 泛型集合實例應(yīng)用的基本內(nèi)容就向你介紹到這里,希望對你了解和學(xué)習(xí)C# 泛型集合有所幫助。

【編輯推薦】

  1. C# 泛型應(yīng)用中屬性淺析
  2. C#泛型操作數(shù)據(jù)庫切換實踐
  3. C# 泛型基礎(chǔ)知識學(xué)習(xí)大全
  4. C# 泛型使用心得淺析
  5. C# 泛型集合概念及應(yīng)用淺析
責(zé)任編輯:仲衡 來源: 51cto.com
相關(guān)推薦

2009-08-24 17:39:21

C# 泛型集合

2009-08-24 18:15:24

C# Dictiona

2009-08-24 15:12:13

C# 泛型接口

2009-08-24 17:27:05

C#泛型應(yīng)用

2009-08-24 16:39:19

C# 泛型應(yīng)用

2009-08-24 11:35:20

C# 泛型應(yīng)用

2009-08-24 10:37:27

C# 泛型

2009-08-24 14:26:42

C# 泛型類

2009-08-24 14:51:25

C# 泛型泛型類型

2009-08-24 15:50:23

C# 泛型C# 泛型委托

2009-08-24 14:20:13

C# 強制類型轉(zhuǎn)換

2009-08-17 17:49:20

C# 枚舉

2009-08-24 13:31:38

C# 泛型約束

2009-08-24 15:28:19

C# 泛型方法

2009-08-24 10:07:57

C#泛型處理

2009-08-24 18:22:05

C# 泛型編程

2009-08-24 16:19:42

C# 泛型方法

2009-08-24 16:01:44

C# 泛型

2009-12-24 09:16:11

C#泛型

2009-08-27 13:05:06

C#接口特點C#接口實例
點贊
收藏

51CTO技術(shù)棧公眾號