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 17:27:05

C#泛型应用

2009-08-24 16:39:19

C# 泛型应用

2009-08-24 11:35:20

C# 泛型应用

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# 强制类型转换

2009-08-17 17:49:20

C# 枚举

2009-08-24 15:28:19

C# 泛型方法

2009-08-24 13:31:38

C# 泛型约束

2009-08-24 18:22:05

C# 泛型编程

2009-08-24 10:07:57

C#泛型处理

2009-12-24 09:16:11

C#泛型

2009-08-24 16:01:44

C# 泛型

2009-08-24 16:19:42

C# 泛型方法

2009-08-28 15:16:18

C#泛型集合
点赞
收藏

51CTO技术栈公众号