C# Collection

In C#,Collection classYesSpecifically used for data storage and retrievalof a type of class.

Collections allow us to easily store, manage, and operate on a set of data, such as:

  • Access items in a list by index;
  • Access values in a dictionary by key;
  • Dynamically add or remove elements, etc.

Collection classes provide support for a variety of common data structures, such as:

  • Stack— Last-In, First-Out (LIFO) structure;
  • Queue— First-In, First-Out (FIFO) structure;
  • List— an ordered sequence that can be dynamically expanded;
  • HashTable— Quickly look up data through key-value pairs.

Furthermore, most collection classes implementSame interface,
This means they have similar operation methods, such as adding, removing, iterating, and so on.

In early C# versions, elements in collections were typically stored asObjectthe object type, and we could put any data into the collection because objectObjects of any type. This is because in C#,Objectis the base class for all data types—whetherint、stringor custom classes, ultimately inherit fromObject。

various collection classes and their usage

Below are various commonly usedSystem.Collectionclasses in the namespace. Click the link below for details.

ClassDescription and usage
Dynamic Array (ArrayList)It represents [objects] that can be individuallyIndexan ordered collection of objects.

It can basically replace an array. However, unlike arrays, you can useIndexAdd and remove items at the specified position, and the dynamic array will automatically resize itself. It also allows dynamic memory allocation, addition, search, and sorting of items in the list.

Hashtableit usesKeyto access elements in the collection.

When you access elements by key, you use a hashtable, and you can identify a useful key value. Each item in a hashtable has akey/valueCorrect. Keys are used to access items in the collection.

SortedListIt can useKeyandIndexto access items in the list.

A sorted list is a combination of an array and a hashtable. It contains a list whose items can be accessed by key or index. If you access items by index, it is a dynamic array (ArrayList); if you access items by key, it is a hashtable (Hashtable). Items in the collection are always sorted by key value.

StackIt represents aLast-in, first-outcollection of objects.

When you need last-in, first-out access to items, use a stack. When you add an item to the list, it is calledPushan element, and when you remove an item from the list, it is calledPopElement.

QueueIt represents afirst-in, first-outcollection of objects.

When you need first-in, first-out access to items, use a queue. When you add an item to the list, it is calledEnqueue, and when you remove an item from the list, it is calledDequeue。

Bit array (BitArray)It represents a value expressed using the values 1 and 0, abinaryArray.

When you need to store bits but do not know the number of bits in advance, use a bit array. You can useInteger Indexto access items from the bit array collection; the index starts at zero.

other extensions