📜  C#-集合

📅  最后修改于: 2020-12-28 05:15:00             🧑  作者: Mango


集合类是用于数据存储和检索的专门类。这些类为堆栈,队列,列表和哈希表提供支持。大多数集合类实现相同的接口。

集合类可用于各种目的,例如为元素动态分配内存以及根据索引访问项目列表等。这些类创建Object类的对象的集合,Object类是C#中所有数据类型的基类。

各种集合类及其用法

以下是System.Collection命名空间的各种常用类。单击以下链接以查看其详细信息。

Sr.No. Class & Description and Useage
1 ArrayList

It represents ordered collection of an object that can be indexed individually.

It is basically an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically. It also allows dynamic memory allocation, adding, searching and sorting items in the list.

2 Hashtable

It uses a key to access the elements in the collection.

A hash table is used when you need to access elements by using key, and you can identify a useful key value. Each item in the hash table has a key/value pair. The key is used to access the items in the collection.

3 SortedList

It uses a key as well as an index to access the items in a list.

A sorted list is a combination of an array and a hash table. It contains a list of items that can be accessed using a key or an index. If you access items using an index, it is an ArrayList, and if you access items using a key , it is a Hashtable. The collection of items is always sorted by the key value.

4 Stack

It represents a last-in, first out collection of object.

It is used when you need a last-in, first-out access of items. When you add an item in the list, it is called pushing the item and when you remove it, it is called popping the item.

5 Queue

It represents a first-in, first out collection of object.

It is used when you need a first-in, first-out access of items. When you add an item in the list, it is called enqueue and when you remove an item, it is called deque.

6 BitArray

It represents an array of the binary representation using the values 1 and 0.

It is used when you need to store the bits but do not know the number of bits in advance. You can access items from the BitArray collection by using an integer index, which starts from zero.