概述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);
相关推荐
gc_229933 分钟前
C#测试调用OpenXml操作word文档的基本用法
c#·word·openxml
almighty274 小时前
C#海康车牌识别实战指南带源码
c#·海康车牌识别·c#实现车牌识别·车牌识别源码·c#车牌识别
SEO-狼术5 小时前
.Net Forms Resize Crack
.net
许泽宇的技术分享5 小时前
Text2Sql.Net架构深度解析:从自然语言到SQL的智能转换之道
sql·架构·.net
c#上位机7 小时前
wpf之TextBlock
c#·wpf
almighty2716 小时前
C# WinForm分页控件实现与使用详解
c#·winform·分页控件·c#分页·winform分页
almighty2717 小时前
C#实现导入CSV数据到List<T>的完整教程
c#·csv·格式转换·c#导入数据·csv数据导入
追逐时光者17 小时前
一款基于 .NET 开源美观、功能丰富的串口调试工具
.net
瀚高PG实验室18 小时前
外网获取瀚高.NET驱动dll方法和使用案例
.net·瀚高数据库
程序猿多布18 小时前
Lua和C#比较
c#·lua