大多数集合都在  System.Collections,System.Collections.Generic两个命名空间。

其中System.Collections.Generic专门用于泛型集合。

针对特定类型的集合类型位于System.Collections.Specialized;命名空间;

线程安全的集合类位于System.Collections.Concurrent;命名空间。

下面是集合和列表实现的接口如下:

一、列表

[Serializable]
[DebuggerTypeProxy(typeof(Mscorlib_CollectionDebugView<>))] [DebuggerDisplay("Count = {Count}")] public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

从这个可以看出,泛型集合List<T>实现了这么多接口,具体接口的信息可以通过工具查看。

二、队列

队列先进先出,一头进一头出,用Queue<T>实现

[Serializable]
[DebuggerTypeProxy(typeof(System_QueueDebugView<>))]
[ComVisible(false)]
[DebuggerDisplay("Count = {Count}")]
public class Queue<T> : IEnumerable<T>, ICollection, IEnumerable

可以看出队列实现了集合的接口,迭代的接口

using System;
using System.Collections.Generic; namespace ConsoleApplication1
{
public class Program
{
static void Main(string[] args)
{
Queue<String> queue = new Queue<string>();
//进队
queue.Enqueue("张三");
queue.Enqueue("李四");
queue.Enqueue("王五");
queue.Enqueue("田六");
queue.Enqueue("赵七"); foreach (String item in queue)
{
Console.WriteLine("foreach迭代:" + item);
} //出队
while (queue.Count > )
{
Console.WriteLine("出队:" + queue.Dequeue());
} Console.Read();
}
}
}

三、栈

栈:从同一边先进后出,用Stack<T>实现

[DebuggerDisplay("Count = {Count}")]
[DebuggerTypeProxy(typeof(System_StackDebugView<>))]
[ComVisible(false)]
public class Stack<T> : IEnumerable<T>, ICollection, IEnumerable

  

栈也是实现了集合接口与迭代接口的

  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. Stack<String> stack = new Stack<string>();
  10. //进栈
  11. stack.Push("张三");
  12. stack.Push("李四");
  13. stack.Push("王五");
  14. stack.Push("田六");
  15. stack.Push("赵七");
  16. foreach (String item in stack)
  17. {
  18. Console.WriteLine("foreach迭代:" + item);
  19. }
  20. //出栈
  21. while (stack.Count > 0)
  22. {
  23. Console.WriteLine("出栈:" + stack.Pop());
  24. }
  25. Console.Read();
  26. }
  27. }
  28. }

四、链表

LinkedList是一个双向链表,链表有个有点,就是在链表中间插入、删除元素很快,但是查找中间与末尾的元素很慢,需要一个节点一个节点的去找。

  1. [Serializable]
  2. [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]
  3. [DebuggerDisplay("Count = {Count}")]
  4. [ComVisible(false)]
  5. public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback

由此可见链表也是有集合的特性的,可以迭代,同时还有链表的特性

  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. LinkedList<String> lList = new LinkedList<string>();
  10. LinkedListNode<String> node = new LinkedListNode<string>("root");
  11. lList.AddFirst(node);
  12. node = lList.AddAfter(node, "张三");
  13. node = lList.AddAfter(node, "李四");
  14. node = lList.AddAfter(node, "王五");
  15. node = lList.AddAfter(node, "田六");
  16. node = lList.AddAfter(node, "赵七");
  17. foreach (String item in lList)
  18. {
  19. Console.WriteLine("foreach迭代:" + item);
  20. }
  21. node = lList.First;
  22. Console.WriteLine("第一个元素:" + node.Value);
  23. node = lList.Last;
  24. Console.WriteLine("最后一个元素:" + node.Value);
  25. Console.Read();
  26. }
  27. }
  28. }

五、有序列表

SortedList采用键-值对存储,键不能重复,并且会根据key进行排序

  1. [Serializable]
  2. [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]
  3. [DebuggerDisplay("Count = {Count}")]
  4. [ComVisible(false)]
  5. public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

可以看出SortedList不仅具有字典的特性,还有集合,迭代的功能

  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Key必须唯一,如果不唯一可以考虑Lookup<TKey,TElement>
  10. SortedList<int, String> sList = new SortedList<int, string>();
  11. sList.Add(100, "张三");
  12. sList.Add(21, "李四");
  13. sList.Add(13, "王五");
  14. sList.Add(44, "田六");
  15. sList.Add(35, "赵七");
  16. foreach (KeyValuePair<int, String> item in sList)
  17. {
  18. Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
  19. }
  20. Console.Read();
  21. }
  22. }
  23. }

六、字典

字典是很复杂的数据结构,允许通过key来查找值,字典可以自由添加、删除元素,没有集合由于移动元素导致的开销。

  1. [Serializable]
  2. [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
  3. [DebuggerDisplay("Count = {Count}")]
  4. [ComVisible(false)]
  5. public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback

可以看出字典也具有集合的特性,可以迭代

  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Key必须唯一
  10. Dictionary<int, String> dict = new Dictionary<int, string>();
  11. dict.Add(11, "张三");
  12. dict.Add(1, "李四");
  13. dict.Add(2, "王五");
  14. dict.Add(16, "田六");
  15. dict.Add(12, "赵七");
  16. foreach (KeyValuePair<int, String> item in dict)
  17. {
  18. Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
  19. }
  20. Console.Read();
  21. }
  22. }
  23. }

说到字典,顺便谈一下有序字典,与有序列表对应;SortedDictionary,SortedList,SortedSet

会根据Key进行排序

  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. //Key必须唯一
  10. SortedDictionary<int, String> dict = new SortedDictionary<int, string>();
  11. dict.Add(11, "张三");
  12. dict.Add(1, "李四");
  13. dict.Add(2, "王五");
  14. dict.Add(16, "田六");
  15. dict.Add(12, "赵七");
  16. foreach (KeyValuePair<int, String> item in dict)
  17. {
  18. Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
  19. }
  20. Console.Read();
  21. }
  22. }
  23. }

七、集

集(Set):包含不重复元素,常用HashSet,SortedSet

  1. [Serializable]
  2. [DebuggerDisplay("Count = {Count}")]
  3. [DebuggerTypeProxy(typeof(HashSetDebugView<>))]
  4. public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable
  1. [Serializable]
  2. [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]
  3. [DebuggerDisplay("Count = {Count}")]
  4. public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. HashSet<String> hSet = new HashSet<string>();
  10. hSet.Add("张三");
  11. hSet.Add("李四");
  12. hSet.Add("王五");
  13. hSet.Add("田六");
  14. hSet.Add("赵七");
  15. foreach (String item in hSet)
  16. {
  17. Console.WriteLine("foreach迭代:" + item);
  18. }
  19. Console.Read();
  20. }
  21. }
  22. }
  1. using System;
  2. using System.Collections.Generic;
  3. namespace ConsoleApplication1
  4. {
  5. public class Program
  6. {
  7. static void Main(string[] args)
  8. {
  9. SortedSet<String> hSet = new SortedSet<string>();
  10. hSet.Add("张三");
  11. hSet.Add("李四");
  12. hSet.Add("王五");
  13. hSet.Add("田六");
  14. hSet.Add("赵七");
  15. foreach (String item in hSet)
  16. {
  17. Console.WriteLine("foreach迭代:" + item);
  18. }
  19. Console.Read();
  20. }
  21. }
  22. }

性能比较:

出自:http://blog.csdn.net/ceclar123/article/details/8655853

C# 各种集合的更多相关文章

  1. java基础集合经典训练题

    第一题:要求产生10个随机的字符串,每一个字符串互相不重复,每一个字符串中组成的字符(a-zA-Z0-9)也不相同,每个字符串长度为10; 分析:*1.看到这个题目,或许你脑海中会想到很多方法,比如判 ...

  2. .Net多线程编程—并发集合

    并发集合 1 为什么使用并发集合? 原因主要有以下几点: System.Collections和System.Collections.Generic名称空间中所提供的经典列表.集合和数组都不是线程安全 ...

  3. 一起学 Java(三) 集合框架、数据结构、泛型

    一.Java 集合框架 集合框架是一个用来代表和操纵集合的统一架构.所有的集合框架都包含如下内容: 接口:是代表集合的抽象数据类型.接口允许集合独立操纵其代表的细节.在面向对象的语言,接口通常形成一个 ...

  4. 编写高质量代码:改善Java程序的151个建议(第5章:数组和集合___建议75~78)

    建议75:集合中的元素必须做到compareTo和equals同步 实现了Comparable接口的元素就可以排序,compareTo方法是Comparable接口要求必须实现的,它与equals方法 ...

  5. java基础_集合List与Set接口

    List接口继承了Collection的方法  当然也有自己特有的方法向指定位置添加元素   add(索引,添加的元素); 移除指定索引的元素   remove(索引) 修改指定索引的元素   set ...

  6. Java基础Collection集合

    1.Collection是所有集合的父类,在JDK1.5之后又加入了Iterable超级类(可以不用了解) 2.学习集合从Collection开始,所有集合都继承了他的方法 集合结构如图:

  7. 轻量级“集合”迭代器-Generator

    Generator是PHP 5.5加入的新语言特性.但是,它似乎并没有被很多PHP开发者广泛采用.因此,在我们了解PHP 7对Generator的改进之前,我们先通过一个简单却显而易见的例子来了解下G ...

  8. Asp.net MVC 传递数据 从前台到后台,包括单个对象,多个对象,集合

    今天为大家分享下 Asp.net MVC 将数据从前台传递到后台的几种方式. 环境:VS2013,MVC5.0框架 1.基本数据类型 我们常见有传递 int, string, bool, double ...

  9. 这些.NET开源项目你知道吗?.NET平台开源文档与报表处理组件集合(三)

    在前2篇文章这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧 和这些.NET开源项目你知道吗?让.NET开源来得更加猛烈些吧!(第二辑)中,大伙热情高涨.再次拿出自己的私货,在.NET平台 ...

  10. python 数据类型 --- 集合

    1. 注意列表和集合的区别 set 列表表现形式: list_1 = [1,3,4];  集合表现形式:set_1= set() list_1 = [1,2,3,4,23,4,2] print(lis ...

随机推荐

  1. SAP Crystal Dashboard Design 2011 win7 x64 安装

    suggest: unZip the setup package to C:\dashboard\  (make sure that the path cannot contain non-unico ...

  2. MYSQL简单安装配置

    有用的URL: http://www.cnblogs.com/zeroone/articles/2298942.html http://blog.csdn.net/h1017597898/articl ...

  3. 【poj2891】同余方程组

    同余方程组 例题1:pku2891Strange Way to Express Integers 中国剩余定理求的同余方程组mod 的数是两两互素的.然而本题(一般情况,也包括两两互素的情况,所以中国 ...

  4. 【HDOJ】3308 LCIS

    线段树,题目感觉比较难,看别人思路做的.还得继续练这个专题. #include <iostream> #include <cstdio> #include <cstrin ...

  5. java学习面向对象之final关键字

    之前我们讲过继承的相关知识了,继承就是子类继承父类的属性和方法并且还可以覆盖父类的方法.但是这样有一个缺陷是什么呢,就是当我们一个类当中涉及一些封装的核心的东西或者对整个系统非常关键的方法或者类的时候 ...

  6. IP分片和TCP分片 MTU和MSS(转)

    IP分片和TCP分片 MTU和MSS(转) 访问原文:http://blog.csdn.net/keyouan2008/article/details/5843388 1,MTU(Maximum Tr ...

  7. bzoj2729

    一看就知道是数学题,考虑插空法由于老师只有两人,所以先对老师进行插空这里考虑两种情况:1.两个老师站在同一处,即两个男生之间站了两个老师这时候需要一个女生站在两个老师之间,再对女生插空,根据乘法原理即 ...

  8. Scala:(2)控制结构

    (1)if else val s=) else -1 (2)循环 ){ r=r*n n-= } ///for 循环 to n) r=r*i //until val s="Hello" ...

  9. Tap.js

    Tap.js A lightweight ‘tap’ event JavaScript plugin

  10. extjs几种常用方法

    1,提交 这种情况一般用于登录界面,也在表单提交应用比较多,大多是一个Button的提交事件,代码为: var loginForm =........; if (loginForm .form.isV ...