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

C# 泛型集合實例應用淺析

開發 后端
C# 泛型集合實例應用不僅向你介紹了C# 泛型集合的概念,還向你介紹了C# 泛型集合實例應用的具體的事宜,希望對你學習C# 泛型集合有所幫助。

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

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

  1. using System.Collections;  
  2.  
  3. class MyList﹤T﹥  
  4. {  
  5. private MyListNode firstNode;//首節點  
  6. private int count;//C# 泛型集合-節點計數  
  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. //增加一個節點  
  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# 泛型集合-刪除一個節點  
  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. //為實現該集合類能用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. //內部節點類  
  151. private class MyListNode  
  152. {  
  153. public T data { getset; }//節點上的元素值  
  154. public MyListNode next { getset; }//節點的下一個節點  
  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# 泛型集合實例應用的基本內容就向你介紹到這里,希望對你了解和學習C# 泛型集合有所幫助。

【編輯推薦】

  1. C# 泛型應用中屬性淺析
  2. C#泛型操作數據庫切換實踐
  3. C# 泛型基礎知識學習大全
  4. C# 泛型使用心得淺析
  5. C# 泛型集合概念及應用淺析
責任編輯:仲衡 來源: 51cto.com
相關推薦

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 16:39:19

C# 泛型應用

2009-08-24 17:27:05

C#泛型應用

2009-08-24 10:37:27

C# 泛型

2009-08-24 11:35:20

C# 泛型應用

2009-08-24 14:26:42

C# 泛型類

2009-08-24 14:51:25

C# 泛型泛型類型

2009-08-24 14:20:13

C# 強制類型轉換

2009-08-24 15:50:23

C# 泛型C# 泛型委托

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技術棧公眾號

主站蜘蛛池模板: 91av视频在线播放 | 久久99精品久久久久久 | 色在线免费 | 草久网 | 亚洲欧美一区二区三区在线 | 国产精品日韩在线观看一区二区 | 成人在线播放网站 | 一区二区三区不卡视频 | 成年人视频免费在线观看 | 一区二区三区在线看 | 欧美成人精品一区二区男人看 | 在线观看av不卡 | 我想看国产一级毛片 | 日韩一区二区三区视频在线观看 | 日本韩国电影免费观看 | 中文字幕三区 | 国产日韩一区二区 | 一级毛片视频 | 欧美日韩精品一区二区三区四区 | jav成人av免费播放 | 免费一区在线 | 日本不卡视频在线播放 | 伊人中文字幕 | 欧美视频二区 | www.av7788.com | 日日摸日日碰夜夜爽2015电影 | 国产福利资源在线 | 国产精品久久久久久久久久久久 | 人人爽人人爽人人片av | 无人区国产成人久久三区 | 成年免费大片黄在线观看一级 | 久草视 | 亚洲精品成人免费 | 午夜免费在线 | 国产亚洲一区二区三区 | 国产精品久久久久久 | 成人影院在线视频 | 视频在线一区二区 | 最新日韩精品 | 久久国产高清 | 人成在线视频 |