平时敲代码,只关注如何使用,没有深入去研究一些本质性的东西,靠死记硬背,不去真正理解,其实最后是很难记住的。
对于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 = 0; i < list.Count; i++) { Console.WriteLine("for:" + i.ToString() + " " + list[i]); } Console.WriteLine("................."); list.RemoveAt(0); 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 > 0) { 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 > 0) { 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(100,"张三"); sortList.Add(21,"李四"); sortList.Add(13,"王五"); sortList.Add(44,"田六"); 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(11, "张三"); dict.Add(1, "李四"); dict.Add(2, "王五"); dict.Add(16, "田六"); dict.Add(13, "赵七"); 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(11, "张三"); sortDict.Add(1, "李四"); sortDict.Add(2, "王五"); sortDict.Add(16, "田六"); sortDict.Add(13, "赵七"); 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); } } } }