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 stack = new Stack();
    stack.Push(1);
    stack.Push("two");
    object top = stack.Pop(); // "two"

    Queue

    • 先进先出 (FIFO) 集合。

    • 支持 EnqueueDequeue 方法。

    • 示例:

      Queue queue = new Queue();
      queue.Enqueue(1);
      queue.Enqueue("two");
      object front = queue.Dequeue(); // 1

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

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

      List

      • 动态数组,可以存储特定类型的对象。

        提供了 Add, Insert, Remove, Sort, Reverse 等方法。

        示例:

        List numbers = new List();
        numbers.Add(1);
        numbers.Add(2);

      HashSet

      • 用于存储唯一元素的集合。

      • 提供了 Add, Remove, Contains 等方法。

      • 示例:

        var hashSet = new HashSet();
        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();
        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();
        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();
        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
        */

      文章转载自: ++Y00++

      原文链接: https://www.cnblogs.com/ayic/p/18334908

      体验地址: 引迈 - JNPF快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构

      相关推荐
      -银雾鸢尾-5 小时前
      C#中的StringBuilder相关方法
      开发语言·c#
      -银雾鸢尾-5 小时前
      C#中结构体与类的区别;抽象类与接口的区别
      开发语言·c#
      广州灵眸科技有限公司5 小时前
      xfce桌面触摸校准:基于灵眸科技EASY-EAl-Orin-Nano
      数据库·windows·科技
      心平气和量大福大9 小时前
      C#-WPF-Window主窗体
      开发语言·c#·wpf
      白露与泡影9 小时前
      Arthas 实战指南:从方法耗时定位到 JVM 变量热修改
      服务器·jvm·c#
      王维同学11 小时前
      [原创][Windows C++]LSA 认证、安全与通知包的注册表枚举
      c++·windows·安全
      YCOSA202512 小时前
      雨晨 Windows 11 IOT 企业版 LTSC 24H2 轻装 2合1 26100.8968
      windows·物联网
      汤姆yu12 小时前
      CodeGeeX 4完整安装与实操使用全指南
      人工智能·windows·ai·智能体·视频模型
      EIP低代码平台14 小时前
      EIP低代码平台系统-字典功能讲解
      低代码·c#·工作流
      吐了啊取名字太难14 小时前
      美颜系统AI修图本地跑并支持Mac、win、安卓、iOS不卡顿
      android·人工智能·windows·数码相机·mac·ai编程