In the C#, Collection classes serve various purposes, such as allocating memory dynamically to elements and accessing a list of items based on an index, etc. These classes create collections of objects of the Object class, which is the base class for all data types in C#.
- store object
- update object
- delete object
- retrieve object
- search object, and
- sort object
You can store objects in an array or collection. The collection has an advantage over the array. The array has a size limit but objects stored in the collection can grow or shrink dynamically.
Types of Collections in C#
There are 3 ways to work with collections which are given below.
- System.Collections.Generic classes
- System.Collections classes (Now deprecated)
- System.Collections.Concurrent classes
1) System.Collections.Generic classes
- List
- Stack
- Queue
- LinkedList
- HashSet
- SortedSet
- Dictionary
- SortedDictionary
- SortedList
2) System.Collections classes
These classes are legacy. It is suggested now to use System.Collections.Generic classes. The System. Collections namespace has the following classes:
- ArrayList
- Stack
- Queue
- Hashtable
3) System.Collections.Concurrent classes
The System.Collections.Concurrent namespace provides classes for thread-safe operations. Now multiple threads will not create problems for accessing the collection items.
- BlockingCollection
- ConcurrentBag
- ConcurrentStack
- ConcurrentQueue
- ConcurrentDictionary
- Partitioner
- Partitioner
- OrderablePartitioner