C#中常用集合类型

在C#中,集合是用于存储和操作一组数据项的数据结构。这些集合通常位于 System.Collections 和 System.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) 集合。

  • 支持 Push 和 Pop 方法。

  • 示例

    Stack stack = new Stack();
    stack.Push(1);
    stack.Push("two");
    object top = stack.Pop(); // "two"

    Queue

    • 先进先出 (FIFO) 集合。

    • 支持 Enqueue 和 Dequeue 方法。

    • 示例:

      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) 集合。

      • 支持 Enqueue 和 Dequeue 方法。

      • 示例:

        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) 集合。

      • 支持 Push 和 Pop 方法。

      • 示例:

        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快速开发平台_低代码开发平台_零代码开发平台_流程设计器_表单引擎_工作流引擎_软件架构

      相关推荐
      flyingonpine15 小时前
      PowerShell脚本数字签名实战:自签名证书、代码签名与信任链验证
      windows·信任链
      用户37215742613517 小时前
      C# 实现 Excel 单元格保护:锁定公式并允许编辑指定区域
      c#·ai编程·编程语言
      goehou21 小时前
      让 AI 替我操作这台电脑,先把手能伸多远定下来
      windows·自动化·权限管理·ai agent
      公子小六1 天前
      基于.NET的Windows窗体编程之WinForms数据持久化(1)
      windows·microsoft·c#·.net·winforms
      三雷科技1 天前
      第6章第4节:自定义分词器
      开发语言·c#
      Nerd(BUG版)1 天前
      鸿蒙API18~26新增V2组件全解析
      microsoft·华为·harmonyos
      zhanghaha13141 天前
      C# 系列教程_12 字符串 string 保姆级系统教程(面向新手,大量示例 + 原理讲解)
      开发语言·数据库·c#
      2601_967987372 天前
      安装 Redis 或 Redis Stack
      linux·windows·redis·macos·安装
      Coder_Shenshen2 天前
      罗克韦尔ACD工程归档离线变量树解析
      算法·c#
      xbzb2 天前
      自定义 yum 软件源:Linux 上 createrepo,Windows 上纯 Python 也能造
      linux·运维·windows·python·yum·createrepo