Introduction to Collections in C#
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. As part of this article, we are going to discuss the following pointers in detail.
So, let’s first understand what is the problem with the traditional array in C#, and then we will discuss how to overcome the problems using collections in C#.
What are the Array and their disadvantages 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.
Following are the limitations of Array in C#:
- The array size is fixed. Once the array is created we can never increase the size of an array. If we want then we can do it manually by creating a new array and copy the old array elements into the new array or by using the Array class Resize method which will do the same thing means to create a new array and copy the old array elements into the new array and then destroy the old array.
- We can never insert an element into the middle of an array
- Deleting or removing elements from the middle of the array.
To overcome the above problems, the Collections are introduced in C# 1.0.
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 than the traditional arrays. The collections in C# are reusable, more powerful, 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 collection which is from .Net framework 1.0 is called simply collections or Non-Generic collections in C#. These collection classes are present inside the System.Collections namespace. The example includes are Stack, Queue, LinkedList, SortedList, ArrayList, HashTable, etc.
Auto-Resizing of collections:
The capacity of a collection increases dynamically i.e. when we keep adding new elements, then the size of the collection keeps increasing automatically. Every collection class has three constructors and the behavior of collections will be as following when created using a different constructor.
- Default Constructor: It Initializes a new instance of the collection class that is empty and has the default initial capacity as zero which becomes four after adding the first element and whenever needed the current capacity becomes double.
- Collection (int capacity): This constructor initializes a new instance of the collection class that is empty and has the specified initial capacity, here also when the requirement comes current capacity doubles.
- Collection (Collection): It Initializes a new instance of the collection class that contains elements copied from the specified collection and that has the same initial capacity as the number of elements copied, here also when the requirement comes current capacity doubles.
In the next article, I am going to discuss the ArrayList 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.