概述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);
相关推荐
__water2 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water2 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water3 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
Java资深爱好者4 小时前
VB.NET中如何利用ASP.NET进行Web开发
前端·asp.net·.net
__water5 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。6 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
Lingbug7 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主7 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎
Echo_Lee07 小时前
C#与Python脚本使用共享内存通信
开发语言·python·c#
__water14 小时前
『功能项目』QFrameWork框架重构OnGUI【63】
c#·unity引擎·重构背包框架