概述C#中各种类型集合的特点

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

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

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

  • ArrayList

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

    • 缺乏类型安全性。

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

    • 示例:

      csharp 复制代码
      ArrayList list = new ArrayList();
      list.Add(1);
      list.Add("two");
  • Hashtable

    • 键值对集合,键必须是 object 类型。

    • 缺乏类型安全性。

    • 提供了 Add, Remove, ContainsKey, ContainsValue 等方法。

    • 示例:

      csharp 复制代码
      Hashtable table = new Hashtable();
      table.Add("key", "value");
  • Stack

    • 后进先出 (LIFO) 集合。

    • 支持 PushPop 方法。

    • 示例:

      csharp 复制代码
      Stack<object> stack = new Stack<object>();
      stack.Push(1);
      stack.Push("two");
      object top = stack.Pop(); // "two"
  • Queue

    • 先进先出 (FIFO) 集合。

    • 支持 EnqueueDequeue 方法。

    • 示例:

      csharp 复制代码
      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 等方法。

    • 示例:

      csharp 复制代码
      List<int> numbers = new List<int>();
      numbers.Add(1);
      numbers.Add(2);
  • HashSet

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

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

    • 示例:

      csharp 复制代码
      HashSet<int> uniqueNumbers = new HashSet<int>();
      uniqueNumbers.Add(1);
      uniqueNumbers.Add(2);
  • Dictionary<TKey, TValue>

    • 键值对集合,键和值都可以是特定类型。

    • 提供了 Add, Remove, TryGetValue, ContainsKey 等方法。

    • 示例:

      csharp 复制代码
      Dictionary<string, int> scores = new Dictionary<string, int>();
      scores.Add("Alice", 90);
      scores.Add("Bob", 80);
  • SortedDictionary<TKey, TValue>

    • 键值对集合,按照键排序。

    • 提供了 Add, Remove, TryGetValue, ContainsKey 等方法。

    • 示例:

      csharp 复制代码
      SortedDictionary<string, int> sortedScores = new SortedDictionary<string, int>();
      sortedScores.Add("Alice", 90);
      sortedScores.Add("Bob", 80);
  • Queue

    • 泛型的先进先出 (FIFO) 集合。

    • 支持 EnqueueDequeue 方法。

    • 示例:

      csharp 复制代码
      Queue<int> queue = new Queue<int>();
      queue.Enqueue(1);
      queue.Enqueue(2);
      int front = queue.Dequeue(); // 1
  • Stack

    • 泛型的后进先出 (LIFO) 集合。

    • 支持 PushPop 方法。

    • 示例:

      csharp 复制代码
      Stack<int> stack = new Stack<int>();
      stack.Push(1);
      stack.Push(2);
      int top = stack.Pop(); // 2
  • LinkedList

    • 双向链表,适合频繁插入和删除的场景。

    • 支持 AddFirst, AddLast, RemoveFirst, RemoveLast 等方法。

    • 示例:

      csharp 复制代码
      LinkedList<int> list = new LinkedList<int>();
      list.AddLast(1);
      list.AddLast(2);
相关推荐
夏子曦6 小时前
C#内存管理深度解析:从栈堆原理到高性能编程实践
开发语言·c#
追逐时光者7 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 57 期(2025年10.1-10.12)
后端·.net
INCerry8 小时前
.NET周刊【9月第4期 2025-09-28】
.net
William_cl10 小时前
C# MVC网页调试的方法
开发语言·c#·mvc
小小的技术员11 小时前
C# 无实体生成JSON字符串
c#·json
曹振华11 小时前
.net 6 signalr
.net·signalr
许泽宇的技术分享11 小时前
从零到一:基于.NET 9.0构建企业级AI智能体对话平台的实战之旅
人工智能·.net·ai智能体·a2a协议·agent framework
IDOlaoluo11 小时前
Microsoft.NET安装步骤详解(.NET Framework/.NET 6/7/8安装教程)
microsoft·.net
贪嘴11 小时前
.NET MCP Server 开发教程
.net