大多数集合都在  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. CPU 定位高

    流程:把线程dump出来,然后分析 1:Threaddump的方法: kill -3 pid     jstack -l pid     jvisualvm中来thread dump 2:找到导致cp ...

  2. Keil工程文件的建立、设置与目标文件的获得

    单片机开发中除必要的硬件外,同样离不开软件,我们写的汇编语言源程序要变为 CPU 可以执行的机器码有两种方法,一种是手工汇编,另一种是机器汇编,目前已极少使用手工 汇编的方法了.机器汇编是通过汇编软件 ...

  3. Lua function 函数

    Lua支持面向对象,操作符为冒号‘:’.o:foo(x) <==> o.foo(o, x). Lua程序可以调用C语言或者Lua实现的函数.Lua基础库中的所有函数都是用C实现的.但这些细 ...

  4. PL/SQL 程序块

    DECLARE BARCODE VARCHAR(50); BEGIN BARCODE := 'A'; IF BARCODE<>'A' then SELECT 1 FROM sam_user ...

  5. 高效的TCP消息发送组件

    目前的.net 架构下缺乏高效的TCP消息发送组件,而这种组件是构建高性能分布式应用所必需的.为此我结合多年的底层开发经验开发了一个.net 下的高效TCP消息发送组件.这个组件在异步发送时可以达到每 ...

  6. Unity Flow distort of screen

    Shader "ScreenWater" {Properties { _MainTex ("Base (RGB)", 2D) = "white&quo ...

  7. How to make apq8084

    1.first of all ,you will meet many problem no excute permisson,pls do : chmod -R +x APQ8084/ 2. buil ...

  8. OpenSource

    SugarCRM 客户关系管理系统(CRM).SugarCRM开源版是一款完全开放源代码的商业开源软件 OPENFILER 基于浏览器的免费网络存储管理实用程序,可以在单一框架中提供基于文件的网络连接 ...

  9. Django的请求流程(url)

    一.Django是怎么处理请求的? 当你通过在浏览器里敲http://127.0.0.1:8000/hello/来访问Hello world消息得时候,Django在后台有些什么动作呢? 所有均开始于 ...

  10. 自己生成非官方iPhone toolchain的头文件

    如果你已经搭建好非官方iPhone toolchain开发包,如果缺少某些头文件,可以用以下方法自己生成. 首先下载class-dump: http://www.codethecode.com/pro ...