VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > temp > C#教程 >
  • C#中的集合

C#中有很多集合类型,比如List<T>,Dictionary<TKey,TValue>,这些是我们常用的,但也有一些少众的场合使用的集合:特征类型的ReadOnly集合,Sort集合;封装特定算法类型的集合:Queue<T>(先进先出),Stack<T>(后进先出),LinkedList<T>(链表,每个元素承上启下);这些集合都是在特定的场合下使用,因为他们的特性限制了他的使用场景。

  • 只读集合:

 

 

复制代码
 1 /// <summary>
 2         /// 只读List
 3         /// </summary>
 4         static void ReadOnlyListTest()
 5         {
 6             IReadOnlyList<string> readOnlyList = new List<string>() { "a", "b", "c" };
 7             foreach (var item in readOnlyList)
 8             {
 9                 Console.WriteLine(item);
10             }
11             /*输出结果
12              a
13              b
14              c
15              */
16         }
17         /// <summary>
18         /// 只读字典
19         /// </summary>
20         static void ReadOnlyDictionaryTest()
21         {
22             var readOnlyDictionary = new ReadOnlyDictionary<int, string>(
23                 new Dictionary<int, string>
24                 {
25                     {5,""},
26                     {1,""},
27                     {10, ""}
28                 });
29 
30             foreach (var item in readOnlyDictionary)
31             {
32                 Console.WriteLine($"{item.Key}~{item.Value}");
33             }
34             /*输出结果
35              5~五
36              1~一
37              10~十
38              */
39         }
复制代码
  • 排序集合:

 

 

复制代码
 1 /// <summary>
 2         /// 排序列表
 3         /// </summary>
 4         static void SortListTest()
 5         {
 6             var sortList = new SortedList<int, string>();
 7             sortList.Add(10, "");
 8             sortList.Add(5, "");
 9             sortList.Add(1, "");
10             Console.WriteLine(sortList.Keys);
11             foreach (var item in sortList)
12             {
13                 Console.WriteLine($"{item.Key}~{item.Value}");
14             }
15             /*输出结果
16              1~一
17              5~五
18              10~十
19              */
20         }
21         /// <summary>
22         /// 排序字典
23         /// </summary>
24         static void SortDictionaryTest()
25         {
26             var sortDic = new SortedDictionary<int, string>();
27             sortDic.Add(10, "");
28             sortDic.Add(5, "");
29             sortDic.Add(1, "");
30             Console.WriteLine(sortDic.Keys);
31             foreach (var item in sortDic)
32             {
33                 Console.WriteLine($"{item.Key}~{item.Value}");
34             }
35             /*输出结果
36              1~一
37              5~五
38              10~十
39             */
40         }
41         /// <summary>
42         /// 排序set,不含重复值
43         /// </summary>
44         static void SortSetTest()
45         {
46             var sortSet = new SortedSet<int>();
47             sortSet.Add(10);
48             sortSet.Add(5);
49             sortSet.Add(1);
50             sortSet.Add(1);
51             foreach (var item in sortSet)
52             {
53                 Console.WriteLine(item);
54             }
55             /*输出结果
56              1
57              5
58              10
59             */
60         }
复制代码
  • 元素访问特性集合

 

 

 

 

 

 

复制代码
 1 /// <summary>
 2         /// 链表:每个元素承上启下
 3         /// </summary>
 4         static void LinkedListTest()
 5         {
 6             var linkedList = new LinkedList<string>();
 7             linkedList.AddLast("2");
 8             linkedList.AddLast("3");
 9             linkedList.AddLast("5");
10 
11             linkedList.AddFirst("1");
12 
13             linkedList.AddBefore(linkedList.Find("5"), "4");
14             foreach (var item in linkedList)
15             {
16                 Console.WriteLine(item);
17             }
18 
19             Console.WriteLine($"2前面的值:{linkedList.Find("2").Previous.Value}");
20             Console.WriteLine($"2后面的值:{linkedList.Find("2").Next.Value}");
21 
22             /*输出结果
23              1
24              2
25              3
26              4
27              5
28              2前面的值:1
29              2后面的值:3
30              */
31         }
32         /// <summary>
33         /// 哈希集合
34         /// </summary>
35         static void HashSetTest()
36         {
37             var hashSet = new HashSet<string>();
38             hashSet.Add("a");
39             hashSet.Add("c");
40             hashSet.Add("b");
41             hashSet.Add("a");
42             hashSet.Add("c");
43             hashSet.Add("b");
44             foreach (var item in hashSet)
45             {
46                 Console.WriteLine(item);
47             }
48             /*输出结果
49              a
50              b
51              c
52              */
53         }
54         /// <summary>
55         /// 队列:先进先出
56         /// </summary>
57         static void QueueTest()
58         {
59             var queue = new Queue<int>();
60             queue.Enqueue(1);
61             queue.Enqueue(2);
62             queue.Enqueue(3);
63             foreach (var item in queue)
64             {
65                 Console.WriteLine(item);
66             }
67             Console.WriteLine($"dequeue元素:{queue.Dequeue()}");
68             /*输出结果
69              1
70              2
71              3
72              dequeue元素:1
73              */
74         }
75         /// <summary>
76         /// 堆栈:后进先出
77         /// </summary>
78         static void StackTest()
79         {
80             var stack = new Stack<int>();
81             stack.Push(1);
82             stack.Push(2);
83             stack.Push(3);
84             foreach (var item in stack)
85             {
86                 Console.WriteLine(item);
87             }
88             //pop元素
89             Console.WriteLine($"pop元素:{stack.Pop()}");
90             /*输出结果
91              3
92              2
93              1
94              pop元素:3
95              */
96         }
复制代码

 

作者:Andy

出处:http://cnblogs.com/andy626



相关教程