概述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);
相关推荐
电商api接口开发2 分钟前
ASP.NET MVC 入门指南四
c#·asp.net·mvc
CHQIUU21 分钟前
跨语言哈希一致性:C# 与 Java 的 MD5 之战?
java·c#·哈希算法
技术拾荒者1 小时前
c#加密证件号的中间部分,改为*号
c#
weixin_423995002 小时前
unity 读取csv
unity·c#
奶油话梅糖2 小时前
JSON配置文件格式全解析与多语言实战指南
编程语言
绿龙术士3 小时前
C#核心知识
c#·.net
wkj0013 小时前
java 和 C#操作数据库对比
java·数据库·c#
码观天工6 小时前
.NET AI Preview 2 发布:从 .NET Aspire 支持到 Qdrant 向量数据库集成,助你快速构建云原生 AI 应用
ai·c#·.net·向量数据库·aspire·qdrant
时光追逐者6 小时前
C#/.NET/.NET Core技术前沿周刊 | 第 36 期(2025年4.21-4.27)
c#·.net·.netcore