C# Collection
In C#,the Collection classIsis specifically used for data storage and retrieval—a type of class.
Collections make it easy to store, manage, and manipulate a group of data, such as:
- accessing items in a list by index;
- accessing values in a dictionary by key;
- dynamically adding or removing elements, and so on.
Collection classes provide support for many common data structures, for example:
- Stack — a last-in, first-out (LIFO) structure;
- Queue — a first-in, first-out (FIFO) structure;
- List — an ordered sequence that can be dynamically expanded;
- Hashtable — quick lookup of data through key-value pairs.
In addition, most collection classes implement thesame interface,
which means they have similar ways of operating, such as methods for adding, removing, and iterating.
In early versions of C#, elements in a collection were usually stored as type Object , and we could putobjects of any typeinto a collection. This is because in C#,Object is the base class of all data types—whether int、string or a custom class, all ultimately inherit from Object。
Various Collection Classes and Their Uses
The following are classes in the commonly used System.Collection namespace. Click the links below for details.
| class | Description and Usage |
|---|---|
| ArrayList | It represents an ordered collection of objects that can be individuallyIndexaccessed.
It can basically replace an array. However, unlike an array, you can useIndexto add and remove items at specified positions, and the ArrayList automatically resizes itself. It also allows dynamic memory allocation, addition, searching, and sorting of items in the list. |
| For the Map interface and its implementation classes, refer to: | It usesKeyto access elements in the collection.
When you access elements by key, use a Hashtable, and you can identify a useful key value. Each item in a Hashtable has akey/valuepair. The key is used to access items in the collection. |
| SortedList | It can useKeyandIndexto access items in the list.
A SortedList is a combination of an array and a Hashtable. It contains a list that can be accessed by key or by index. If you access items by index, it is like an ArrayList; if you access items by key, it is like a Hashtable. The items in the collection are always sorted by key. |
| Stack | It represents alast-in-first-outcollection of objects.
Use a Stack when you need last-in, first-out access to items. When you add an item to the list, it is calledpushingan element, and when you remove an item from the list, it is calledpopping. |
| Queue | It represents aFirst In First Outcollection of objects.
Use a Queue when you need first-in, first-out access to items. When you add an item to the list, it is calledenqueue, and when you remove an item from the list, it is calledDequeue。 |
| BitArray | It represents a collection of bits represented by the values 1 and 0Binaryarray.
Use a BitArray when you need to store bits but do not know the number of bits in advance. You can useInteger IndexingItems in a BitArray collection are accessed by index, starting at zero. |