概述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);
相关推荐
缺点内向1 天前
C#: 高效移动与删除Excel工作表
开发语言·c#·.net·excel
yue0081 天前
C# 分部类读取学生信息
开发语言·c#
聪明努力的积极向上1 天前
【C#】事件简单解析
开发语言·c#
qq_12498707531 天前
基于C#的贵州省黔北地区乡村避暑生活共享平台设计与实现(源码+论文+部署+安装)
c#·毕业设计·asp.net·生活
LateFrames1 天前
C# 中,0.1 在什么情况下不等于 0.1 ?
开发语言·c#
一抓掉一大把2 天前
.net实现秒杀商品(Redis高并发)
.net
San302 天前
深入理解JavaScript执行机制:从变量提升到内存管理
javascript·编程语言·代码规范
mudtools2 天前
解放双手!使用Roslyn生成代码让你的 HTTP 客户端开发变得如此简单
低代码·c#·.net
张人玉2 天前
WPF 数据绑定与转换器详解
c#·wpf·light
主宰者2 天前
WPF CalcBinding简化判断逻辑
c#·.net·wpf