C#常用集合的使用
大多数集合都在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>实现了这么多接口,具体接口的信息可以通过工具查看。
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- List<String> list = new List<string>();
- list.Add("张三");
- list.Add("李四");
- list.Add("王五");
- list.Add("田六");
- list.Add("赵七");
- for (int i = 0; i < list.Count; i++)
- {
- Console.WriteLine("for循环:" + i.ToString() + "=" + list[i]);
- }
- list.RemoveAt(0);
- foreach (String item in list)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- list.AddRange(new String[] { "Hello1", "Hello2", "Hello3" });
- list.ForEach(Print);
- Console.Read();
- }
- private static void Print(String item)
- {
- Console.WriteLine("ForEach:" + item);
- }
- }
- }
二、队列
队列先进先出,一头进一头出,用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 > 0)
- {
- 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
栈也是实现了集合接口与迭代接口的
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- Stack<String> stack = new Stack<string>();
- //进栈
- stack.Push("张三");
- stack.Push("李四");
- stack.Push("王五");
- stack.Push("田六");
- stack.Push("赵七");
- foreach (String item in stack)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- //出栈
- while (stack.Count > 0)
- {
- Console.WriteLine("出栈:" + stack.Pop());
- }
- Console.Read();
- }
- }
- }
四、链表
LinkedList是一个双向链表,链表有个有点,就是在链表中间插入、删除元素很快,但是查找中间与末尾的元素很慢,需要一个节点一个节点的去找。
- [Serializable]
- [DebuggerTypeProxy(typeof(System_CollectionDebugView<>))]
- [DebuggerDisplay("Count = {Count}")]
- [ComVisible(false)]
- public class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
由此可见链表也是有集合的特性的,可以迭代,同时还有链表的特性
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- LinkedList<String> lList = new LinkedList<string>();
- LinkedListNode<String> node = new LinkedListNode<string>("root");
- lList.AddFirst(node);
- node = lList.AddAfter(node, "张三");
- node = lList.AddAfter(node, "李四");
- node = lList.AddAfter(node, "王五");
- node = lList.AddAfter(node, "田六");
- node = lList.AddAfter(node, "赵七");
- foreach (String item in lList)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- node = lList.First;
- Console.WriteLine("第一个元素:" + node.Value);
- node = lList.Last;
- Console.WriteLine("最后一个元素:" + node.Value);
- Console.Read();
- }
- }
- }
五、有序列表
SortedList采用键-值对存储,键不能重复,并且会根据key进行排序
- [Serializable]
- [DebuggerTypeProxy(typeof(System_DictionaryDebugView<,>))]
- [DebuggerDisplay("Count = {Count}")]
- [ComVisible(false)]
- public class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable
可以看出SortedList不仅具有字典的特性,还有集合,迭代的功能
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //Key必须唯一,如果不唯一可以考虑Lookup<TKey,TElement>
- SortedList<int, String> sList = new SortedList<int, string>();
- sList.Add(100, "张三");
- sList.Add(21, "李四");
- sList.Add(13, "王五");
- sList.Add(44, "田六");
- sList.Add(35, "赵七");
- foreach (KeyValuePair<int, String> item in sList)
- {
- Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
- }
- Console.Read();
- }
- }
- }
六、字典
字典是很复杂的数据结构,允许通过key来查找值,字典可以自由添加、删除元素,没有集合由于移动元素导致的开销。
- [Serializable]
- [DebuggerTypeProxy(typeof(Mscorlib_DictionaryDebugView<,>))]
- [DebuggerDisplay("Count = {Count}")]
- [ComVisible(false)]
- public class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable, ISerializable, IDeserializationCallback
可以看出字典也具有集合的特性,可以迭代
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //Key必须唯一
- Dictionary<int, String> dict = new Dictionary<int, string>();
- dict.Add(11, "张三");
- dict.Add(1, "李四");
- dict.Add(2, "王五");
- dict.Add(16, "田六");
- dict.Add(12, "赵七");
- foreach (KeyValuePair<int, String> item in dict)
- {
- Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
- }
- Console.Read();
- }
- }
- }
说到字典,顺便谈一下有序字典,与有序列表对应;SortedDictionary,SortedList,SortedSet
会根据Key进行排序
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- //Key必须唯一
- SortedDictionary<int, String> dict = new SortedDictionary<int, string>();
- dict.Add(11, "张三");
- dict.Add(1, "李四");
- dict.Add(2, "王五");
- dict.Add(16, "田六");
- dict.Add(12, "赵七");
- foreach (KeyValuePair<int, String> item in dict)
- {
- Console.WriteLine("key=" + item.Key.ToString() + ";value=" + item.Value);
- }
- Console.Read();
- }
- }
- }
七、集
集(Set):包含不重复元素,常用HashSet,SortedSet
- [Serializable]
- [DebuggerDisplay("Count = {Count}")]
- [DebuggerTypeProxy(typeof(HashSetDebugView<>))]
- public class HashSet<T> : ISerializable, IDeserializationCallback, ISet<T>, ICollection<T>, IEnumerable<T>, IEnumerable
- [Serializable]
- [DebuggerTypeProxy(typeof(SortedSetDebugView<>))]
- [DebuggerDisplay("Count = {Count}")]
- public class SortedSet<T> : ISet<T>, ICollection<T>, IEnumerable<T>, ICollection, IEnumerable, ISerializable, IDeserializationCallback
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- HashSet<String> hSet = new HashSet<string>();
- hSet.Add("张三");
- hSet.Add("李四");
- hSet.Add("王五");
- hSet.Add("田六");
- hSet.Add("赵七");
- foreach (String item in hSet)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- Console.Read();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- namespace ConsoleApplication1
- {
- public class Program
- {
- static void Main(string[] args)
- {
- SortedSet<String> hSet = new SortedSet<string>();
- hSet.Add("张三");
- hSet.Add("李四");
- hSet.Add("王五");
- hSet.Add("田六");
- hSet.Add("赵七");
- foreach (String item in hSet)
- {
- Console.WriteLine("foreach迭代:" + item);
- }
- Console.Read();
- }
- }
- }
性能比较:

---------------------------------------------------
C#常用集合的使用的更多相关文章
- 比较Java中几个常用集合添加元素的效率
初始化需要进行比较的集合,统一增加10万个元素,获取整个过程的执行时间. 1.List集合增加元素 private static void testList() { List<Integer&g ...
- .NET基础 (09)常用集合和泛型
常用集合和泛型1 int[]是引用类型还是值类型2 数组之间如何进行转换3 解释泛型的基本原理4 什么是泛型的主要约束和次要约束 常用集合和泛型1 int[]是引用类型还是值类型 数组类型是一族类型, ...
- C#常用集合
数组的缺点:长度固定.因此引入集合的使用. 注:泛型集合更安全,性能更高. 常用集合 对应泛型 ①动态数组ArrayList List<T> 常用方法属性:Add Clear C ...
- golang实现常用集合原理介绍
golang本身对常用集合的封装还是比较少的,主要有数组(切片).双向链表.堆等.在工作中可能用到其他常用的集合,于是我自己对常用的集合进行了封装,并对原理做了简单介绍,代码库地址:https://g ...
- Java常用集合笔记
最近事情比较少,闲暇之余温习巩固一下Java的一些基础知识,并做一些笔记, Java常用集合, 主要参考的这篇文章:Java常用集合 ArrayList/Vertor 1. ArrayList 的主要 ...
- Java中常用集合操作
一.Map 名值对存储的. 常用派生类HashMap类 添加: put(key,value)往集合里添加数据 删除: clear()删除所有 remove(key)清除单个,根据k来找 获取: siz ...
- java常用集合详解 contains
java集合是对常用数据集合的封装,差不多就是数组吧,验证某个元素是否在数据集合里,最原始的方法是,用个循环,"某个元素"与数据集合中的每个元素逐个进行比较. java 对常用的一 ...
- C#常用集合的使用(转载)
大多数集合都在System.Collections,System.Collections.Generic两个命名空间.其中System.Collections.Generic专门用于泛型集合. 针对特 ...
- JAVA常用集合源码解析系列-ArrayList源码解析(基于JDK8)
文章系作者原创,如有转载请注明出处,如有雷同,那就雷同吧~(who care!) 一.写在前面 这是源码分析计划的第一篇,博主准备把一些常用的集合源码过一遍,比如:ArrayList.HashMap及 ...
随机推荐
- Redis高可用配置(Keepalived)
主:172.16.0.104 备:172.16.0.105 VIP:172.16.0.107 客户端直接连VIP,当master 104的redis挂掉后,105作为master.当104重启后,10 ...
- kali Rolling 安装QQ
------------------------------------------------------------------- 环境: kali Rolling 64位 所需软件包: Wi ...
- AR9531的mac地址
root@OpenWrt:/# ifconfigbr-lan Link encap:Ethernet HWaddr 00:11:22:34:56:78 inet addr ...
- P2P直连?经服务器中转?
当同一个系统的两个客户端A.B相互发送消息给对方时,如果它们之间存在P2P通道,那么消息传送的路径就有两种:直接经P2P通道传送.或者经服务器中转.如下图所示: 通常就一般应用而言,如果P2P通道能够 ...
- 评测:VPS推荐digitalocean和Vultr和Linode
美国vps推荐三家,分别是digitalocean.vultr和linode,拥有很高的性价比,中国访问速度快.我是上面三家的早期用户,并且一直使用至今,积累了不少使用经验. DigitalOcean ...
- NOIP2011-普及组复赛模拟试题-第二题-买票
题目背景 Background Ztc真的遇上黄牛了... 题目描述 Description 周末Ztc想去剧场看演出,但是他没有票.这时,救世主Wzj出现了,他慷慨地愿意卖给Ztc一些票. ...
- redis高级实用特性(2)
事务处理: redis对事务的支持目前还是比较简单,redis只能保证一个 client发起事务中的命令可以连续执行,而中间不会插入其他 client的命令,当一个client 在一个连接中发出mul ...
- iOS 加载本地的html文件
方法1: self.webView = [[UIWebView alloc]initWithFrame:self.view.bounds]; NSString *path = [[NSBundle ...
- User already has more than 'max_user_connections' active connections
vi /etc/my.cnf修改 max_user_connections 参数的数值,重启 MySQL 服务器
- 九、oracle 事务
一.什么是事务事务用于保证数据的一致性,它由一组相关的dml语句组成,该组的dml(数据操作语言,增删改,没有查询)语句要么全部成功,要么全部失败.如:网上转账就是典型的要用事务来处理,用于保证数据的 ...