Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Collection of C#


May 11, 2021 C#


Table of contents


The collection of C#

Collection classes are classes that are designed for data storage and retrieval. T hese classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement the same interface.

Collection classes serve different purposes, such as dynamically allocating memory for elements, index-based access to list items, and so on. T hese classes create a collection of objects for the Object class. The Object class is the base class for all data types in C#.

Various collection classes and their usage

Below are the classes for the various commonly used System.Collection namespaces. Click on the link below to see the details.

描述和用法
动态数组(ArrayList) 它代表了可被单独 索引 的对象的有序集合。

它基本上可以替代一个数组。但是,与数组不同的是,您可以使用 索引 在指定的位置添加和移除项目,动态数组会自动重新调整它的大小。它也允许在列表中进行动态内存分配、增加、搜索、排序各项。

哈希表(Hashtable) 它使用 来访问集合中的元素。

当您使用键访问元素时,则使用哈希表,而且您可以识别一个有用的键值。哈希表中的每一项都有一个 键/值 对。键用于访问集合中的项目。

排序列表(SortedList) 它可以使用 索引 来访问列表中的项。

排序列表是数组和哈希表的组合。它包含一个可使用键或索引访问各项的列表。如果您使用索引访问各项,则它是一个动态数组(ArrayList),如果您使用键访问各项,则它是一个哈希表(Hashtable)。集合中的各项总是按键值排序。

堆栈(Stack) 它代表了一个 后进先出 的对象集合。

当您需要对各项进行后进先出的访问时,则使用堆栈。当您在列表中添加一项,称为 推入 元素,当您从列表中移除一项时,称为 弹出 元素。

队列(Queue) 它代表了一个 先进先出 的对象集合。

当您需要对各项进行先进先出的访问时,则使用队列。当您在列表中添加一项,称为 入队 ,当您从列表中移除一项时,称为 出队

点阵列(BitArray) 它代表了一个使用值 1 和 0 来表示的 二进制 数组。

当您需要存储位,但是事先不知道位数时,则使用点阵列。您可以使用 整型索引 从点阵列集合中访问各项,索引从零开始。