C#中常用集合类型

在C#中,集合是用于存储和操作一组数据项的数据结构。这些集合通常位于 System.CollectionsSystem.Collections.Generic 命名空间中。下面我将概述C#中几种常用的集合类型及其特点:

1. System.Collections 命名空间中的集合

这个命名空间中的集合类型不支持泛型,因此在编译时不检查类型安全性。这意味着在运行时可能会遇到类型转换错误。

  • ArrayList

    • 动态数组,可以存储任意类型的对象。
    • 缺乏类型安全性。
    • 提供了 Add, Insert, Remove, Sort, Reverse 等方法。
    • 示例:
      ArrayList list = new ArrayList();
      list.Add(1);
      list.Add("two");
      
  • Hashtable

    • 键值对集合,键必须是 object 类型。
    • 键必须唯一。
    • 缺乏类型安全性。
    • 提供了 Add, Remove, ContainsKey, ContainsValue 等方法。
    • 示例:
      Hashtable table = new Hashtable();
      table.Add("key", "value");
      
  • Stack

    • 后进先出 (LIFO) 集合。
    • 支持 PushPop 方法。
    • 示例:
      Stack<object> stack = new Stack<object>();
      stack.Push(1);
      stack.Push("two");
      object top = stack.Pop(); // "two"
      
  • Queue

    • 先进先出 (FIFO) 集合。
    • 支持 EnqueueDequeue 方法。
    • 示例:
      Queue<object> queue = new Queue<object>();
      queue.Enqueue(1);
      queue.Enqueue("two");
      object front = queue.Dequeue(); // 1
      

2. System.Collections.Generic 命名空间中的集合

这个命名空间中的集合类型支持泛型,因此可以确保类型安全性。

  • List

    • 动态数组,可以存储特定类型的对象。
    • 提供了 Add, Insert, Remove, Sort, Reverse 等方法。
    • 示例:
      List<int> numbers = new List<int>();
      numbers.Add(1);
      numbers.Add(2);
      
  • HashSet

    • 用于存储唯一元素的集合。
    • 提供了 Add, Remove, Contains 等方法。
    • 示例:
      var hashSet = new HashSet<string>();
      hashSet.Add("a");
      hashSet.Add("c");
      hashSet.Add("b");
      hashSet.Add("a");
      hashSet.Add("c");
      hashSet.Add("b");
      foreach (var item in hashSet)
      {
      	Console.WriteLine(item);
      }
      /*输出结果
       a
       b
       c
       */
      
  • Dictionary<TKey, TValue>

    • 键值对集合,键和值都可以是特定类型。
    • 键必须唯一。
    • 提供了 Add, Remove, TryGetValue, ContainsKey 等方法。
    • 示例:
      Dictionary<string, int> scores = new Dictionary<string, int>();
      scores.Add("Alice", 90);
      scores.Add("Bob", 80);
      
  • SortedDictionary<TKey, TValue>

    • 键值对集合,按照键排序。
    • 键必须唯一。
    • 提供了 Add, Remove, TryGetValue, ContainsKey 等方法。
    • 示例:
      var sortDic = new SortedDictionary<int, string>();
      sortDic.Add(10, "十");
      sortDic.Add(5, "五");
      sortDic.Add(1, "一");
      Console.WriteLine(sortDic.Keys);
      foreach (var item in sortDic)
      {
      	Console.WriteLine($"{item.Key}~{item.Value}");
      }
      /*输出结果
       1~一
       5~五
       10~十
       */
      
  • Queue

    • 泛型的先进先出 (FIFO) 集合。
    • 支持 EnqueueDequeue 方法。
    • 示例:
      var queue = new Queue<int>();
      queue.Enqueue(1);
      queue.Enqueue(2);
      queue.Enqueue(3);
      foreach (var item in queue)
      {
      	Console.WriteLine(item);
      }
      Console.WriteLine($"dequeue元素:{queue.Dequeue()}");
      /*输出结果
       1
       2
       3
       dequeue元素:1
       */
      
  • Stack

    • 泛型的后进先出 (LIFO) 集合。
    • 支持 PushPop 方法。
    • 示例:
      var stack = new Stack<int>();
      stack.Push(1);
      stack.Push(2);
      stack.Push(3);
      foreach (var item in stack)
      {
      	Console.WriteLine(item);
      }
      //pop元素
      Console.WriteLine($"pop元素:{stack.Pop()}");
      /*输出结果
       3
       2
       1
       pop元素:3
       */
      
  • LinkedList

    • 双向链表,适合频繁插入和删除的场景。
    • 支持 AddFirst, AddLast, RemoveFirst, RemoveLast 等方法。
    • 示例:
      var linkedList = new LinkedList<string>();
      linkedList.AddLast("2");
      linkedList.AddLast("3");
      linkedList.AddLast("5");
      
      linkedList.AddFirst("1");
      linkedList.AddBefore(linkedList.Find("5"), "4");
      
      foreach (var item in linkedList)
      {
      	Console.WriteLine(item);
      }
      
      Console.WriteLine($"2前面的值:{linkedList.Find("2").Previous.Value}");
      Console.WriteLine($"2后面的值:{linkedList.Find("2").Next.Value}");
      
      /*输出结果
       1
       2
       3
       4
       5
       2前面的值:1
       2后面的值:3
       */
      

 

2024-08-13 22:10:00【出处】:https://www.cnblogs.com/ayic/p/18334908

=======================================================================================

posted on 2024-08-13 22:11  jack_Meng  阅读(7)  评论(0编辑  收藏  举报

导航