经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » C# » 查看文章
概述C#中各种类型集合的特点
来源:cnblogs  作者:Y00  时间:2024/7/31 16:19:10  对本文有异议

image

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

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

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

  • ArrayList

    • 动态数组,可以存储任意类型的对象。
    • 缺乏类型安全性。
    • 提供了 Add, Insert, Remove, Sort, Reverse 等方法。
    • 示例:
      1. ArrayList list = new ArrayList();
      2. list.Add(1);
      3. list.Add("two");
  • Hashtable

    • 键值对集合,键必须是 object 类型。
    • 缺乏类型安全性。
    • 提供了 Add, Remove, ContainsKey, ContainsValue 等方法。
    • 示例:
      1. Hashtable table = new Hashtable();
      2. table.Add("key", "value");
  • Stack

    • 后进先出 (LIFO) 集合。
    • 支持 PushPop 方法。
    • 示例:
      1. Stack<object> stack = new Stack<object>();
      2. stack.Push(1);
      3. stack.Push("two");
      4. object top = stack.Pop(); // "two"
  • Queue

    • 先进先出 (FIFO) 集合。
    • 支持 EnqueueDequeue 方法。
    • 示例:
      1. Queue<object> queue = new Queue<object>();
      2. queue.Enqueue(1);
      3. queue.Enqueue("two");
      4. object front = queue.Dequeue(); // 1

2. System.Collections.Generic 命名空间中的集合

这个命名空间中的集合类型支持泛型,因此可以确保类型安全性。

  • List

    • 动态数组,可以存储特定类型的对象。
    • 提供了 Add, Insert, Remove, Sort, Reverse 等方法。
    • 示例:
      1. List<int> numbers = new List<int>();
      2. numbers.Add(1);
      3. numbers.Add(2);
  • HashSet

    • 用于存储唯一元素的集合。
    • 提供了 Add, Remove, Contains 等方法。
    • 示例:
      1. HashSet<int> uniqueNumbers = new HashSet<int>();
      2. uniqueNumbers.Add(1);
      3. uniqueNumbers.Add(2);
  • Dictionary<TKey, TValue>

    • 键值对集合,键和值都可以是特定类型。
    • 提供了 Add, Remove, TryGetValue, ContainsKey 等方法。
    • 示例:
      1. Dictionary<string, int> scores = new Dictionary<string, int>();
      2. scores.Add("Alice", 90);
      3. scores.Add("Bob", 80);
  • SortedDictionary<TKey, TValue>

    • 键值对集合,按照键排序。
    • 提供了 Add, Remove, TryGetValue, ContainsKey 等方法。
    • 示例:
      1. SortedDictionary<string, int> sortedScores = new SortedDictionary<string, int>();
      2. sortedScores.Add("Alice", 90);
      3. sortedScores.Add("Bob", 80);
  • Queue

    • 泛型的先进先出 (FIFO) 集合。
    • 支持 EnqueueDequeue 方法。
    • 示例:
      1. Queue<int> queue = new Queue<int>();
      2. queue.Enqueue(1);
      3. queue.Enqueue(2);
      4. int front = queue.Dequeue(); // 1
  • Stack

    • 泛型的后进先出 (LIFO) 集合。
    • 支持 PushPop 方法。
    • 示例:
      1. Stack<int> stack = new Stack<int>();
      2. stack.Push(1);
      3. stack.Push(2);
      4. int top = stack.Pop(); // 2
  • LinkedList

    • 双向链表,适合频繁插入和删除的场景。
    • 支持 AddFirst, AddLast, RemoveFirst, RemoveLast 等方法。
    • 示例:
      1. LinkedList<int> list = new LinkedList<int>();
      2. list.AddLast(1);
      3. list.AddLast(2);

原文链接:https://www.cnblogs.com/ayic/p/18334908

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号