大多数集合都在System.Collections,System.Collections.Generic两个命名空间。其中System.Collections.Generic专门用于泛型集合。

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

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

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

一、列表

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

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

  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. List<String> list = new List<string>();
  10. list.Add("张三");
  11. list.Add("李四");
  12. list.Add("王五");
  13. list.Add("田六");
  14. list.Add("赵七");
  15. for (int i = 0; i < list.Count; i++)
  16. {
  17. Console.WriteLine("for循环:" + i.ToString() + "=" + list[i]);
  18. }
  19. list.RemoveAt(0);
  20. foreach (String item in list)
  21. {
  22. Console.WriteLine("foreach迭代:" + item);
  23. }
  24. list.AddRange(new String[] { "Hello1", "Hello2", "Hello3" });
  25. list.ForEach(Print);
  26. Console.Read();
  27. }
  28. private static void Print(String item)
  29. {
  30. Console.WriteLine("ForEach:" + item);
  31. }
  32. }
  33. }

二、队列

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

  1. [Serializable]
  2. [DebuggerTypeProxy(typeof(System_QueueDebugView<>))]
  3. [ComVisible(false)]
  4. [DebuggerDisplay("Count = {Count}")]
  5. public class Queue<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. Queue<String> queue = new Queue<string>();
  10. //进队
  11. queue.Enqueue("张三");
  12. queue.Enqueue("李四");
  13. queue.Enqueue("王五");
  14. queue.Enqueue("田六");
  15. queue.Enqueue("赵七");
  16. foreach (String item in queue)
  17. {
  18. Console.WriteLine("foreach迭代:" + item);
  19. }
  20. //出队
  21. while (queue.Count > 0)
  22. {
  23. Console.WriteLine("出队:" + queue.Dequeue());
  24. }
  25. Console.Read();
  26. }
  27. }
  28. }

三、栈

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

  1. [DebuggerDisplay("Count = {Count}")]
  2. [DebuggerTypeProxy(typeof(System_StackDebugView<>))]
  3. [ComVisible(false)]
  4. 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. }

性能比较:

---------------------------------------------------

C#常用集合的使用的更多相关文章

  1. 比较Java中几个常用集合添加元素的效率

    初始化需要进行比较的集合,统一增加10万个元素,获取整个过程的执行时间. 1.List集合增加元素 private static void testList() { List<Integer&g ...

  2. .NET基础 (09)常用集合和泛型

    常用集合和泛型1 int[]是引用类型还是值类型2 数组之间如何进行转换3 解释泛型的基本原理4 什么是泛型的主要约束和次要约束 常用集合和泛型1 int[]是引用类型还是值类型 数组类型是一族类型, ...

  3. C#常用集合

    数组的缺点:长度固定.因此引入集合的使用. 注:泛型集合更安全,性能更高. 常用集合 对应泛型 ①动态数组ArrayList    List<T> 常用方法属性:Add  Clear  C ...

  4. golang实现常用集合原理介绍

    golang本身对常用集合的封装还是比较少的,主要有数组(切片).双向链表.堆等.在工作中可能用到其他常用的集合,于是我自己对常用的集合进行了封装,并对原理做了简单介绍,代码库地址:https://g ...

  5. Java常用集合笔记

    最近事情比较少,闲暇之余温习巩固一下Java的一些基础知识,并做一些笔记, Java常用集合, 主要参考的这篇文章:Java常用集合 ArrayList/Vertor 1. ArrayList 的主要 ...

  6. Java中常用集合操作

    一.Map 名值对存储的. 常用派生类HashMap类 添加: put(key,value)往集合里添加数据 删除: clear()删除所有 remove(key)清除单个,根据k来找 获取: siz ...

  7. java常用集合详解 contains

    java集合是对常用数据集合的封装,差不多就是数组吧,验证某个元素是否在数据集合里,最原始的方法是,用个循环,"某个元素"与数据集合中的每个元素逐个进行比较. java 对常用的一 ...

  8. C#常用集合的使用(转载)

    大多数集合都在System.Collections,System.Collections.Generic两个命名空间.其中System.Collections.Generic专门用于泛型集合. 针对特 ...

  9. JAVA常用集合源码解析系列-ArrayList源码解析(基于JDK8)

    文章系作者原创,如有转载请注明出处,如有雷同,那就雷同吧~(who care!) 一.写在前面 这是源码分析计划的第一篇,博主准备把一些常用的集合源码过一遍,比如:ArrayList.HashMap及 ...

随机推荐

  1. Unity3D研究院之在把代码混淆过的游戏返混淆回来(七十七)

    最近一直在找如何在MAC上混淆Android的DLL,至今没能找到合适的,有大神知道记得告诉我喔.今天群里有人说了一个混淆代码和返混淆代码的工具de4dot ,不查不知道一查吓一跳.这玩意可以把别人混 ...

  2. Win10安装安卓ADB驱动

    Win10安装安装ADB驱动 Step1: 首先在黄色感叹号的ADB Interface 点右键菜单,选择“更新驱动程序软件”菜单. 在弹出“更新驱动程序软件”窗口中,选择下面一项“浏览计算机以查找驱 ...

  3. 打开新世界的第一步:学习servlet

    什么是servlet? 是用Java编写的服务器端程序.其主要功能在于交互式地浏览和修改数据,生成动态Web内容. 创建servlet 一.创建一个web project 1)流程:eclipse-F ...

  4. NameCheap域名注册商的几个特点介绍

    作为站长,我们拥有选择3-5家域名注册商,把自己的域名放置在不同的平台也是应该的,因为不同的注册商都有不同的值得我们选择的理由.作为已经使用namecheap注册商超过5年,拥有数百个域名的我来说,分 ...

  5. “System.BadImageFormatException”类型的未经处理的异常在 PurchaseDevices.Access.dll 中发生 其他信息: 未能加载文件或程序集“System.Data.SQLite, Version=1.0.66.0, Culture=neutral, PublicKeyToken=db937bc2d44ff139”或它的某一个依赖项。试图加载格式不正确

    引用sqlite的程序集时,有时会报如下异常:  "System.BadImageFormatException"类型的未经处理的异常在 PurchaseDevices.Acces ...

  6. 第二章App框架设计与重构

    response标准格式: { "isError":false, "errorType": 0, "errorMessage": " ...

  7. 淘淘商城_day05_课堂笔记

    今日大纲 学习Redis 使用Redis完成项目中缓存需求 实现商品详情页功能 缓存的需求 大广告位数据无需每次查询后台系统的接口,可以在前台系统添加缓存,提高访问首页的速度. 商品类目的数据也可以缓 ...

  8. Spring 后置处理器 PropertyPlaceholderConfigurer 类(引用外部文件)

    一.PropertyPlaceholderConfigurer类的作用 PropertyPlaceholderConfigurer 是 BeanFactory 后置处理器的实现,也是 BeanFact ...

  9. python 强制结束线程的坑

    网上流传了两种能强制结束线程的错误姿势 第一种:通过setDaemon来结束线程 http://www.cnblogs.com/jefferybest/archive/2011/10/09/22040 ...

  10. 配置IIS提示打开目录浏览时的问题:未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的解决办法

    错误消息: 未能从程序集“System.ServiceModel, Version=3.0.0.0”中加载类型“System.ServiceModel.Activation.HttpModule” 的 ...