In this article, I am going to give you a brief Introduction to Collections in C#. Please read our previous article where we discussed the Advantages and Disadvantages of Arrays in C# with Examples. Collections are similar to Arrays, it provides a more flexible way of working with a group of objects. As part of this article, we are going to discuss the following pointers in detail.
Collections are nothing but groups of records that can be treated as one logical unit. For example, you can have a country collection that can have a country code and country name. You can have a product collection that has the Product Id and Product Name. You can have an employee collection having the employee’s name and employee id. You can have a book collection having an ISBN number and book name. For a better understanding, please have a look at the below image.
Collections are classified into 4 types such as Indexed Based, Key-Value Pair, Prioritized Collection, and Specialized Collections. For a better understanding, please have a look at the below diagram.
In Indexed Based, we have two kinds of collections i.e. Array and List. To understand the Indexed Based collection, please have a look at the following country collection. So, when we add any elements to .NET Collections like Array, List, or ArrayList, it maintains its own internal index number. This internal index number is auto-generated by the framework and using this index number we can identify the records.
In the Key-Value Pair collection, we have Hashtable, Dictionary, and SortedList. In real-time projects, we rarely accessed the records using the internal index numbers. We generally use some kind of keys to identify the records and there we use the Key-Value Pair collections like Hashtable, Dictionary, and SortedList. For a better understanding, please have a look at the below diagram.
So, basically, if we want to fetch the records based on a key, then we need to use Key-Value Pair collections such as Hashtable, Dictionary, and SortedList.
The Prioritized Collections help you to access the elements in a particular sequence. The Stack and Queue collections belong to the Prioritized Collections category. If you want First in First Out (FIFO) access to the elements of a collection, then you need to use Queue collection. On the other hand, if you want Last in First Out (LIFO) access to the elements of a collection, then you need to use the Stack collection. For a better understanding, please have a look at the below image.
The Specialized Collections are specifically designed for a specific purpose. For example, a Hybrid Dictionary starts as a list and then becomes a hashtable.
Now, let us understand what are the problems with the Traditional Array in C#, and how we can overcome such problems using collections in C#.
In simple words, we can say that the Arrays in C# are the simple data structure that is used to store similar types of data items in sequential order. Although the arrays in C# are commonly used, they have some limitations.
For example, you need to specify the array’s size while creating the array. If at execution time, you want to modify it that means if you want to increase or decrease the size of an array, then you need to do it manually by creating a new array or by using the Array class’s Resize method, which internally creates a new array and copies the existing array element into the new array.
To overcome the above problems, the Collections are introduced in C# 1.0.
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.
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.
There are 3 ways to work with collections. The three namespaces are given below:
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:
SortedList: It represents a collection of key/value pairs that are sorted by the keys and are accessible by key and by index.
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:
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:
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.