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

对于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. ZOJ3362 Beer Problem(最小费用任意流)

    题目大概说有n个城市,由m条无向边相连,每条边每天最多运送cap桶酒且其运送一桶的花费是cost.现在从1号城市开始出发运酒,供应到2到n号城市,这些城市的收购单价是price,问最大的盈利是多少. ...

  2. LightOJ1025 The Specials Menu(区间DP)

    给一个字符串,问有几种删字符的方式使删后的非空字符串是个回文串. 当然区间DP:dp[i][j]表示子串stri...strj的方案数 感觉不好转移,可能重复算了.我手算了"AAA" ...

  3. BZOJ3847 : ZCC loves march

    注意到集结操作相当于合并一些点 于是我们可以使用并查集 对于每一行.每一列维护一个链表,储存里面的点 查询x时,在并查集中找到x的祖先u,此时x的坐标就是u的坐标 然后扫描u所在行列的链表,依次删除每 ...

  4. web farm 讨论引出

    关于web farm 有成功的实施的文档没 用它还不如 用nginx,简单易用. Nginx for windows的运行效果咋样 windows  iis无敌 玩nginx就不要用win系统,必须l ...

  5. Windows Phone7 快递查询

        (1)API去友商100里申请 布局代码: Exp.xaml <phone:PhoneApplicationPage x:Class="WindowsPhone_Express ...

  6. FFmpeg - 音频解码过程

    1. 注册所有解码器 av_register_all(); 2. Codec & CodecContext AVCodec* codec = avcodec_find_decoder(CODE ...

  7. Idea_idea代码调试debug篇

    主要看图,看图一目了然. 断点的设定和eclipse一样,只要点一下就可以,下面是我设定的几个断点,再下面的三个窗口是用来调试代码的,这个和eclipse类似 调试常用的快捷键 ? 1 2 3 4 5 ...

  8. Dynamic LINQ OrderBy

    public static IOrderedQueryable<T> OrderBy<T>(this IQueryable<T> source, string pr ...

  9. 发布android app到android market的方法

      转载自: http://www.stwind.org/android-market 给你的程序签名注意事项:所有提交到Market的程序必须经过签名.未经签名的程序不能安装.你可以使用个人证书去签 ...

  10. php读取3389脚本

    <?php $regkey = 'HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Terminal Server\Wds\rdpwd\Td ...