06 Feb 2024
Intermediate
In .NET C#, there are various types of collections provided by the System.Collections and System.Collections.Generic namespaces. Here are some commonly used types of collections:
-
Non-Generic Collections (in
System.Collectionsnamespace):- ArrayList: A dynamically resizable array that can hold elements of different types.
- Hashtable: Stores key/value pairs in a hash table, providing fast lookups based on keys.
- Queue: Represents a first-in, first-out (FIFO) collection of objects.
- Stack: Represents a last-in, first-out (LIFO) collection of objects.
-
Generic Collections (in
System.Collections.Genericnamespace):- List<T>: A dynamic array that can grow or shrink and stores elements of a specified type.🔗Read more
- Dictionary<K, V>: Stores key/value pairs and provides fast lookup based on keys.🔗Read more
- HashSet<T>: Represents a collection of unique elements, ensuring no duplicates..🔗Read more
- Queue<T>: Represents a first-in, first-out (FIFO) collection of objects.🔗Read more
- Stack<T>: Represents a last-in, first-out (LIFO) collection of objects.🔗Read more
- LinkedList<T>: A doubly linked list that allows efficient insertion and removal of elements. 🔗Read more
- SortedDictionary<K, V>: Similar to a dictionary but maintains keys in sorted order. 🔗Read more
- SortedList<K, V>: Similar to a sorted dictionary but implemented using an array-based list. 🔗Read more
-
Specialized Collections (in various namespaces):
- ObservableCollection<T>: A dynamic data collection that provides notifications when items get added, removed, or when the collection is refreshed.
- KeyedCollection<K, T>: Represents a collection of objects that can be accessed by a key, similar to a dictionary.
-
Concurrent Collections (in
System.Collections.Concurrentnamespace):- ConcurrentBag<T>: Represents an unordered collection of elements.
- ConcurrentDictionary<K, V>: Represents a thread-safe dictionary.
- ConcurrentQueue<T>: Represents a thread-safe first-in, first-out (FIFO) collection.
- ConcurrentStack<T>: Represents a thread-safe last-in, first-out (LIFO) collection.
These collections serve various purposes and are designed for different use cases. The choice of collection type depends on the specific requirements of your application, including performance considerations, the need for ordering, the presence of duplicates, and thread safety requirements.
c-sharp
collections