大多数集合都在  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. 对话机器学习大神Yoshua Bengio(下)

    对话机器学习大神Yoshua Bengio(下) Yoshua Bengio教授(个人主页)是机器学习大神之一,尤其是在深度学习这个领域.他连同Geoff Hinton老先生以及 Yann LeCun ...

  2. iOS内存管理系列之一:对象所有权与引用计数

    当一个所有者(owner,其本身可以是任何一个Objective-C对象)做了以下某个动作时,它拥有对一个对象的所有权(ownership): 1. 创建一个对象.包括使用任何名称中包含“alloc” ...

  3. DJANGO不同应用之间的用户迁移

    因为重新规划新的项目,数据库表结构和以前不一定了,但是想保存以前的很多用户认证方面的东东. 于是看了一下DJANGO的导入导出功能. ~~~~~~~~~~~~~~~~~~~ 数据导入: python ...

  4. Django自身的CBV列表

    慢慢就有感觉了.... 这个是可以快速开发很多东东,不过,类视图要求的积累还是有一些的.. ~~~~~~~~~~~~~ CBVs在功能上的可扩展性, 牺牲的是简单性, 一个CBV最多的时候拥有8个im ...

  5. CABAC

    CABAC(Context-based Adaptive Binary Arithmetic Coding),基于上下文的自适应二进制算术编码.CABAC是H.264/AVC标准中两种熵编码中的一种, ...

  6. C语言嵌入式系统编程修炼之一:背景篇

    不同于一般形式的软件编程,嵌入式系统编程建立在特定的硬件平台上,势必要求其编程语言具备较强的硬件直接操作能力.无疑,汇编语言具备这样的特质.但是,归因于汇编语言开发过程的复杂性,它并不是嵌入式系统开发 ...

  7. Java ftp断点续传

    FtpTransFile类 import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundExcept ...

  8. 14.5.5.1 An InnoDB Deadlock Example 一个InnoDB 死锁实例

    14.5.5.1 An InnoDB Deadlock Example 一个InnoDB 死锁实例 下面的例子演示了一个错误可以发生当一个lock 请求会导致一个死锁,例子设计2个客户端,A和B: J ...

  9. scrollView的用法

    - (void)viewDidLoad {     [super viewDidLoad];           scrollView = [[UIScrollView alloc] initWith ...

  10. JavaScript高级程序设计6.pdf

    ECMAScript通过RegExp类型来支持正则表达式 var expression=/pattern/flags;其中模式(pattern)部分是正则表达式,可以包含字符类.限定符.分组.向前查找 ...