C# Collections
What is a Collection in C#?
The Collections in C# are a set of predefined classes that are present in the System.Collections namespace that provides greater capabilities and functionalities than the traditional arrays. The collections in C# are reusable, more powerful, and more efficient and most importantly they have been designed and tested to ensure quality and performance.
So in simple words, we can say a Collection in C# is a dynamic array. That means the collections in C# have the capability of storing multiple values but with the following features.
- Size can be increased dynamically.
- We can insert an element into the middle of a collection.
- It also provides the facility to remove or delete elements from the middle of a collection.
The collections in C# are classes that represent a group of objects. With the help of C# Collections, we can perform different types of operations on objects such as Store, Update, Delete, Retrieve, Search, and Sort objects, etc. In short, all the data structure work can be performed by collections in C#. That means Collections standardize the way in which the objects are handled by our program.
Types of Collections in C#
There are 3 ways to work with collections. The three namespaces are given below:
- System.Collections classes
- System.Collections.Generic classes
- System.Collections.Concurrent classes
Non-Generic Collections Classes in C#:
The Non-Generic Collection Classes come with C# 1.0 and are defined under the System.Collections namespace. The Non-Generic collection classes in C# operate on objects, and hence can handle any type of data, but not in a safe-type manner. The System.Collections namespace contains the following classes:
- ArrayList: It Implements the System.Collections.IList interface using an array whose size is dynamically increased as required.
- Stack: It represents a simple last-in-first-out (LIFO) non-generic collection of objects.
- Queue: It represents a first-in, first-out collection of objects.
- Hashtable: It represents a collection of key/value pairs that are organized based on the hash code of the key.
SortedList: It represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
Generic Collections Classes in C#:
The Generic Collection Classes come with C# 2.0 and are defined under the System.Collection.Generic namespace. It provides a generic implementation of standard data structures like linked lists, stacks, queues, and dictionaries. These collection classes are type-safe because they are generic means only those items that are type-compatible with the type of the collection can be stored in a generic collection, it eliminates accidental type mismatches. The System.Collections.Generic namespace has the following classes:
- List<T>: It represents a strongly typed list of objects that can be accessed by index. Provides methods to search, sort, and manipulate lists.
- Stack<T>: It represents a variable size last-in-first-out (LIFO) collection of instances of the same specified type.
- Queue<T>: It represents a first-in, first-out collection of objects.
- HashSet<T>: It represents a set of values. It removes duplicate elements from the collection.
- Dictionary<TKey, TValue>: It represents a collection of keys and values.
- SortedList<TKey, TValue>: It represents a collection of key/value pairs that are sorted by key based on the associated System.Collections.Generic.IComparer implementation.
- SortedSet<T>: It represents a collection of objects that are maintained in sorted order.
- SortedDictionary<TKey, TValue>: It represents a collection of key/value pairs that are sorted on the key.
- LinkedList<T>: It represents a doubly linked list.
Concurrent Collection Classes in C#:
It came in .NET Framework Version 4 and onwards. It provides various threads-safe collection classes that are used in place of the corresponding types in the System.Collections and System.Collections.Generic namespaces, when multiple threads are accessing the collection simultaneously. The System.Collections.Concurrent namespace provides classes for thread-safe operations. Now multiple threads will not create problems for accessing the collection items. The System.Collections.Concurrent namespace has the following classes:
- BlockingCollection<T>: It provides blocking and bounding capabilities for thread-safe collections that implement System.Collections.Concurrent.IProducerConsumerCollection.
- ConcurrentBag<T>: It represents a thread-safe, unordered collection of objects.
- ConcurrentStack<T>: It represents a thread-safe last-in-first-out (LIFO) collection.
- ConcurrentQueue<T>: It represents a thread-safe first-in-first-out (FIFO) collection.
- ConcurrentDictionary<TKey, TValue>: It represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently.
In the next article, I am going to discuss the Non-Generic ArrayList Collection Class in C# with Examples. Here, in this article, I give you a brief introduction to Collections in C#. I hope this article will help you with your needs. I would like to have your feedback. Please post your feedback, question, or comments about this article.
Comments
Post a Comment