平时敲代码,只关注如何使用,没有深入去研究一些本质性的东西,靠死记硬背,不去真正理解,其实最后是很难记住的。

对于C#常见的集合,自己平时好像只有用到List,Dictionary,ArrayList,Array等几个,其实C#的集合远远不止这几个,

一直认为Dictionary是有序集合,哎,错了好久,今天总算明白了。

C#中的集合主要放在System.Collections和System.Collections.Generic这个两个命名空间下,其中System.Collections.Generic专门用于泛型集合,如下图:

关于各类集合的差异,网上已有不少大牛做过总结,参考如下:

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

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ConsoleApplication_List
{
class Program
{
static void Main(string[] args)
{
ListTest();
QueueTest();
StackTest();
LinkedListTest();
SortedListTest();
DictionaryTest();
SortDictionaryTest();
SetTest();
SortedSetTest(); Console.Read();
} #region MyRegion /// <summary>
/// 列表
/// </summary>
static void ListTest()
{
Console.WriteLine("List"); List<string> list = new List<string>();
list.Add("张三");
list.Add("李四");
list.Add("王五"); for (int i = ; i < list.Count; i++)
{
Console.WriteLine("for:" + i.ToString() + " " + list[i]);
} Console.WriteLine("................."); list.RemoveAt();
foreach (var item in list)
{
Console.WriteLine("Foreach:" + item);
} Console.WriteLine("................."); list.AddRange(new string[] { "demo1", "demo2", "demo3" });
list.ForEach(PrintEach); Console.WriteLine("................."); } private static void PrintEach(string item)
{
Console.WriteLine("Foreach:" + item);
} #endregion /// <summary>
/// 队列
/// </summary>
static void QueueTest()
{
Console.WriteLine("Queue"); Queue<string> queue = new Queue<string>();
queue.Enqueue("张三");
queue.Enqueue("李四");
queue.Enqueue("王五");
queue.Enqueue("田六");
queue.Enqueue("赵七"); Console.WriteLine("................."); foreach (string item in queue)
{
Console.WriteLine("foreach迭代:" + item);
} Console.WriteLine("................."); while (queue.Count > )
{
Console.WriteLine("出队:" + queue.Dequeue());
}
} /// <summary>
/// 栈
/// </summary>
public static void StackTest()
{
Console.WriteLine("stack"); 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);
}
Console.WriteLine("................."); while (stack.Count > )
{
Console.WriteLine("出栈:" + stack.Pop());
}
Console.WriteLine(".................");
} /// <summary>
/// 链表
/// </summary>
public static void LinkedListTest()
{
Console.WriteLine("LinkedList"); LinkedList<string> linkedList = new LinkedList<string>();
LinkedListNode<string> node = new LinkedListNode<string>("root");
linkedList.AddFirst(node);
linkedList.AddAfter(node, "张三");
linkedList.AddAfter(node, "李四");
linkedList.AddAfter(node, "王五");
linkedList.AddAfter(node, "田六"); foreach (string item in linkedList)
{
Console.WriteLine("foreach迭代:" + item);
}
Console.WriteLine("................."); node = linkedList.First;
Console.WriteLine("第一个元素:" + node.Value);
node = linkedList.Last;
Console.WriteLine("最后一个元素:" + node.Value);
Console.WriteLine(".................");
} /// <summary>
/// 有序列表
/// </summary>
public static void SortedListTest()
{
Console.WriteLine("SortedList"); SortedList<int, string> sortList = new SortedList<int, string>();
sortList.Add(,"张三");
sortList.Add(,"李四");
sortList.Add(,"王五");
sortList.Add(,"田六"); foreach (KeyValuePair<int, string> item in sortList)
{
Console.WriteLine("key=" + item.Key + ";value=" + item.Value);
}
} /// <summary>
/// 字典
/// </summary>
public static void DictionaryTest()
{
Console.WriteLine("Dictionary"); Dictionary<int, string> dict = new Dictionary<int, string>();
dict.Add(, "张三");
dict.Add(, "李四");
dict.Add(, "王五");
dict.Add(, "田六");
dict.Add(, "赵七"); foreach (KeyValuePair<int, string> item in dict)
{
Console.WriteLine("key=" + item.Key + ";value=" + item.Value);
}
} /// <summary>
/// 有序字典
/// </summary>
public static void SortDictionaryTest()
{
Console.WriteLine("SortDictionary"); SortedDictionary<int, string> sortDict = new SortedDictionary<int, string>();
sortDict.Add(, "张三");
sortDict.Add(, "李四");
sortDict.Add(, "王五");
sortDict.Add(, "田六");
sortDict.Add(, "赵七"); foreach (KeyValuePair<int, string> item in sortDict)
{
Console.WriteLine("key=" + item.Key + ";value=" + item.Value);
}
} /// <summary>
/// 集
/// </summary>
public static void SetTest()
{
Console.WriteLine("HashSet"); 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);
}
} /// <summary>
/// 有序集
/// </summary>
public static void SortedSetTest()
{
Console.WriteLine("HashSet"); SortedSet<string> sortSet = new SortedSet<string>();
sortSet.Add("张三");
sortSet.Add("李四");
sortSet.Add("王五");
sortSet.Add("田六");
sortSet.Add("赵七"); foreach (String item in sortSet)
{
Console.WriteLine("foreach迭代:" + item);
}
}
}
}

C#中集合汇总的更多相关文章

  1. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  2. javascript中event汇总

    原文:javascript中event汇总 ie firefox chrome opera Safari浏览器中对 event的处理并不一致,在此我将各个浏览器中event的兼容处理做了一个汇总,此处 ...

  3. Java中集合List,Map和Set的区别

    Java中集合List,Map和Set的区别 1.List和Set的父接口是Collection,而Map不是 2.List中的元素是有序的,可以重复的 3.Map是Key-Value映射关系,且Ke ...

  4. .Net中集合排序的一种高级玩法

    背景: 学生有名称.学号, 班级有班级名称.班级序号 学校有学校名称.学校编号(序号) 需求 现在需要对学生进行排序 第一排序逻辑 按学校编号(序号)排列 再按班级序号排列 再按学生学号排列 当然,在 ...

  5. Python中集合set()的使用及处理

    在Python中集合(set)与字典(dict)比较相似,都具有无序以及元素不能重复的特点 1.创建set 创建set需要一个list或者tuple或者dict作为输入集合 重复的元素在set中会被自 ...

  6. java中集合Collection转list对象

    参考:java中集合Collection转list对象 首先我的需求是获取到购物车列表,购物车列表是一个Map对象,构造方法获取购物项,这里购物项是Collection对象 // 购物项集合,K商品I ...

  7. C# 中集合类型需要按多个条件排序

    在 C# (.net 3.5 之后) 中集合是可以通过 OrderBy() 和 OrderByDescending()方法来进行排序的,如果需要集合中的元素是对象,还可以通过 Lambda表达式进行按 ...

  8. (转)CloudStack 安装及使用过程中常见问题汇总

    CloudStack 安装及使用过程中常见问题汇总             在做工程项目中对CloudStack 安装及使用过程中常见的几个问题及如何解决做一个总结.   1.Windows XP虚拟 ...

  9. Matlab中插值函数汇总(上)

    Matlab中插值函数汇总分上下两个部分,主要整合自matlabsky论坛dynamic发表于2009-2-21 21:53:26 的主题帖,以及豆丁网rickoon上传的教材第8章<插值,拟合 ...

随机推荐

  1. Gradle dsl method not found renderscriptSupportMode()

    连接: How to use the Renderscript Support Library with Gradle Android-Studio and Renderscript support ...

  2. UOJ#61. 【UR #5】怎样更有力气

    大力水手问禅师:“大师,很多事情都需要用很大力气才能完成,而我在吃了菠菜之后力气很大,于是就导致我现在非常依赖菠菜.我很讨厌我的现状,有没有办法少吃点菠菜甚至不吃菠菜却仍很有力气?” 禅师浅笑,答:“ ...

  3. android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到

    android BroadcastReceiver ACTION_TIME_TICK 系统时间监听不到 今天做android上的消息推送,启动了一个独立service,然后在里面监听系统的ACTION ...

  4. QDir, QFileInfo 和 QDirIterator 区别

    这三个类相互有关联,但是有不尽相同,首先从名字上看,QDir 和 QDirIterator 是针对于文件目录的,也就是文件夹,我们知道,对于一个文件夹,可以包含很多文件,也可以包含其他文件夹,通常是一 ...

  5. hdu I NEED A OFFER!

    这道题是道很基本的0/1背包的问题,为了使解题很简单一点,可以将题目中要求的最大概率转换成不能录取的最小概率,这样1-dp[n]即为至少有一个offer的最大概率.状态方程 为:dp[j]=min{d ...

  6. safedog的小技巧

    限制3389连接:下载SafedogServer\SafeDogGuardCenter\ProGuardData.ini回本地,然后本地搭建安全狗,覆盖,查看计算机名,修改自己计算机名再连接. 卸载安 ...

  7. checking在浏览器为应用缓存查找更新时触发

    离线的Web应用,就是在设备不能上网的时候还能运行应用.html5把离线应用作为重点,主要是开发人员的心愿.离线应用的开发的步骤有:首先应该知道设备是否能够上网;然后应该还能访问一定的资源(如图像.C ...

  8. [转]Web Service Authentication

    本文转自:http://www.codeproject.com/Articles/9348/Web-Service-Authentication Download source files - 45. ...

  9. 通过Sysprep封装系统

    <?xml version="1.0" encoding="utf-8"?> <unattend xmlns="urn:schema ...

  10. iisNS 安装程序的思路

    1. 安装程序的目录和 frontEnd一个目录,通过判断是否存在 common/config/db.php 来验证是否已经安装过,如果已经安装过,该文件会自动生成 2.  如果没有安装过,则跳转到 ...