Collections in C-Sharp - Everything you need to know
With the help of data types in a programming language, users can store basic data such as text, number, and Boolean values. These are considered to be the built-in datatypes that can be used to store the values of the user's choice.
Consider the following example. A company wishes to build an application that collects the final bill value of all the sales made in a day. Now it is not possible to get the entire list with the help of basic data types that are present in a programming language. The users need a special data type that can hold multiple entries of the basic data types together to achieve the required functionalities. One such special data type that is available in C# is called collections.
In this post, we will understand what is a collection and also look into the different types of collection and how do they vary from each other.
Table of contents
- What is a collection in C#?
- Classification of collections in C#
- Available collections in C#
- Wrapping up
What is a collection in C#?
Collections are special classes that provide a standardized way to store the elements. Using the collections, users can perform operations such as add, update, delete, retrieve, search, sort, etc..,
These collections come in handy when there are projects that require operations on multiple entries of data. Collections are built-in with the C# programming language.
Classification of collections in C#
Collections in C# are classified into two types - Generic collections and non-generic collections.
In C#, generic collections are available under the namespace ‘System.Collections.Generic’. When you consider performance, Generic collections are faster over non-generic collections. Also, they are type-safe and have lesser runtime exceptions. This is because most of the exceptions can be found and handled during the compile time. On top of this, Generic collections have more in-built functionalities over non-generic collections.
Below is the list of generic collections available in C# -
In C#, non-generic collections are available under the namespace ‘System.Collections’. It can handle any type of object and it is not type-safe. Also, users will be able to add different data types of elements in a non-generic collection.
Below is the list of few non-generic collections available in C# -
Available collections in C#
In this section, let's see about the list of collections available in C#
The list comes with in-built methods to add an element to the list, add multiple elements to the list, sort, search, and remove element(s) from the list.
To create and initialize a list of strings, you can use the below line of code -
var exampleStringList = new List<string>();
Dictionary class is a generic collection and a basic implementation of IDictionary<TKey, TValue>. It can hold multiple elements with a key and its respective value. It also allows users to add, remove, and search the records based on the key or the value. And it doesn't allow duplication of the keys that are getting stored. The size of the dictionary dynamically changes based on the elements.
To create and initialize a dictionary which holds the key as a string and value as a string, you can use the below line of code -
var exampleDictionary = new Dictionary<string,string>();
HashSet class is a generic collection and it is similar to list. HashSet can be used to store unordered unique elements. It is specifically used in scenarios where duplicates need to be avoided. Performance-wise it is faster when compared to the list.
HashSet does not store the elements in any particular order. And similar to list, its capacity changes based on the addition or removal operations.
To create and initialize a HashSet of integers, you can use the below line of code -
var exampleHashset = new HashSet<int>();
ArrayList class is a non-generic collection. ArrayList can be used to add unknown data types and the size of the data. ArrayList inherits the IList interface, so it supports accessing the elements using an indexer.
They are not type-safe, as they allow all data types. So, users can add a string, a number, Boolean value, and null to a single object of an ArrayList.
ArrayList supports inbuilt method such as foreach, insert, insert range, remove, removerange, etc.,
To create and initialize an ArrayList object, you can use the below line of code -
var exampleArraylist = new ArrayList();
Hashtable class is a non-generic collection. It can store data in the form of a key-value pair. And it can store key-value pairs of the same type or different type in the collection.
Hashtable doesn't require the type to be specified explicitly as it accepts all the data types. And when compared to the dictionary, the process of data retrieval is slower, as it has to do boxing/unboxing. And accessing a key that doesn't exist in the hashtable returns null.
To create and initialize a hashtable, you can use the below line of code-
var exampleHashtable = new Hashtable();
SortedList is a collection of key-value pair which are by default sorted in the ascending order of the keys. The specialty of SortedList is that it is present in both generic and non-generic collections.
The keys in the SortedList cannot be null and it doesn't allow duplicate keys to be stored.
To create and initialize a SortedList, you can use the below line of code-
var exampleSortedList = new SortedList<int,string>();
Collections form an important data structure through which the users can handle their application data. .Net framework provides various collection types including list, ArrayList, dictionary, HashSet, hashtable, etc. The collections namespace includes both concrete and interface types which can be used for creating new collection types.