In C#, a collection is a data structure used to store and manipulate a set of data items. These collections are usually located in the cap (a poem)
in the namespace. Below I will outline a few common collection types in C# and their characteristics:
1.
Collections in namespaces
The collection types in this namespace do not support generics, so type safety is not checked at compile time. This means that type conversion errors may be encountered at runtime.
-
ArrayList
- Dynamic arrays that can store objects of any type.
- Lack of type safety.
- offers
Add
,Insert
,Remove
,Sort
,Reverse
and other methods. - Example:
ArrayList list = new ArrayList(); (1); ("two");
-
Hashtable
- A collection of key-value pairs where the keys must be
object
Type. - Lack of type safety.
- offers
Add
,Remove
,ContainsKey
,ContainsValue
and other methods. - Example:
Hashtable table = new Hashtable(); ("key", "value");
- A collection of key-value pairs where the keys must be
-
Stack
- Last In First Out (LIFO) collections.
- be in favor of
Push
cap (a poem)Pop
Methods. - Example:
Stack<object> stack = new Stack<object>(); (1); ("two"); object top = (); // "two"
-
Queue
- First-in, first-out (FIFO) collections.
- be in favor of
Enqueue
respond in singingDequeue
Methods. - Example:
Queue<object> queue = new Queue<object>(); (1); ("two"); object front = (); // 1
2.
Collections in namespaces
The collection types in this namespace support generalization, thus ensuring type safety.
-
List
- Dynamic arrays that can store objects of a specific type.
- offers
Add
,Insert
,Remove
,Sort
,Reverse
and other methods. - Example:
List<int> numbers = new List<int>(); (1); (2);
-
HashSet
- A collection used to store unique elements.
- offers
Add
,Remove
,Contains
and other methods. - Example:
HashSet<int> uniqueNumbers = new HashSet<int>(); (1); (2);
-
Dictionary<TKey, TValue>
- A collection of key-value pairs where both keys and values can be of a specific type.
- offers
Add
,Remove
,TryGetValue
,ContainsKey
and other methods. - Example:
Dictionary<string, int> scores = new Dictionary<string, int>(); ("Alice", 90); ("Bob", 80);
-
SortedDictionary<TKey, TValue>
- A collection of key-value pairs, sorted by key.
- offers
Add
,Remove
,TryGetValue
,ContainsKey
and other methods. - Example:
SortedDictionary<string, int> sortedScores = new SortedDictionary<string, int>(); ("Alice", 90); ("Bob", 80);
-
Queue
- Generalized First-In-First-Out (FIFO) collections.
- be in favor of
Enqueue
cap (a poem)Dequeue
Methods. - Example:
Queue<int> queue = new Queue<int>(); (1); (2); int front = (); // 1
-
Stack
- Generalized last-in-first-out (LIFO) collections.
- be in favor of
Push
cap (a poem)Pop
Methods. - Example:
Stack<int> stack = new Stack<int>(); (1); (2); int top = (); // 2
-
LinkedList
- Bidirectional chained tables for frequent insertion and deletion scenarios.
- be in favor of
AddFirst
,AddLast
,RemoveFirst
,RemoveLast
and other methods. - Example:
LinkedList<int> list = new LinkedList<int>(); (1); (2);