Collections in C# is a must-know concept for every developer, whether entry level or an experienced one. There are some of the topics in Collections which are getting advanced with the release of new version of C#. So, let's learn about Collections.
What is “Collections” in C#?
Simply, Collection is a set of objects of similar type, grouped together.
All the collections implement IEnumrable interface which is inherited from ICollection interface. We can say that IEnumrable is the mother of all types of collections present in .NET.
- Standard Collection: This is found under system.Collections namespace.
- Generic Collection: This is found under system.Collections.Generic namespace. These are more flexible to work with data
- Index based: Array , List
- Key value pair: Hashtable , Dictionary
- Prioritized Collection: Stack & Queues
- Specialized Collection: String Collection
Working with Array
- Arrays are strongly typed entities.
- It’s a fixed size entity/type which means, if we define an array of size 10, then it will have the space allocated for 10 elements and will need to loop through to find where the data is filled or not.
- It’s a sequential collection which will be of the same type.
- Elements can be accessed by using the index of an element.
- Each index is initialized with a default value depending on the type of array created.
Working with ArrayList
- It is a sophisticated version of an array.
- It is one of the generic collection types present in System.Collection.Generic namespace.
- An ArrayList can be used to create a collection of any type, such as String, Int16 etc., and even complex types.
- The object stored in the list can be accessed by an Index.
- Unlike arrays, List can grow its size automatically
- List has methods to search, sort, and manipulate the list.
- It uses both,the equality comparer as well as the ordering comparer.
- List class is a strongly typed class.
Difference between Array and ArrayList
- Arrays are used when the collection is of fixed length.
- Array is faster than ArrayList because array doesn't require boxing / un-boxing.
- Arrays are always strongly typed as compared to ArrayList.
- ArrayList can grow automatically.
Working With Hashtable
- It is a collection of Key-value pairs that are organized on the hash code of the key.
- It uses key to link the elements in the Collection.
- Key can be of any type.
- Key cannot be null.
- Value against a key can be null.
- It is not strongly typed.
- Strong type of Hashtable is called Dictionary.
Difference between ArrayList and Hashtable
- Hashtable uses the key to access a specific element in a Collection whereas the ArrayList uses index which is zero based.
- ArrayList is faster than Hashtable because there is no hashcoding involved in it.
Working With Generic Collections
Generic separates the logic from the data type.
There are different variations of Collections, such as hash tables, queues, stacks, dictionaries, and ArrayList. There are some sorted generic Collections, as well. The element of those can be accessed by using a key as well as an index. But, for Generic Collection like Hashtable and Dictionaries, elements can be accessed using a key only. Every Collection has a method to add, remove, and search an element function present in the below namespace.
Working with Collection Interfaces : IEnumrable, IList, ICollection, IDictionary
These interfaces help us achieve two big concepts of OOPS- Encapsulation and Polymorphism.
IEnumerator
- This helps to iterate over the Collection, without exposing the add, remove methods as in ArrayList, and only helps in browsing the Collection.
- It has a method called GetEnumrator which returns IEnumerator object that helps to iterate over Collection using foreach loop.
- Every .NET Collection implements IEnumerable interface.
ICollection
- This helps to iterate over the Collection, without exposing the add, remove methods as in ArrayList, and also helps in browsing the Collection.
- It is inherited from IEnumrable Interface.
- It has extra count property which helps getting the count of elements in the Collection.
IList: This helps to iterate over the Collection along with having the add & remove functionality.
IDictionary: This helps to iterate over the Collection with a key-value pair and also provides with add & remove functionality.
IList
- This helps to iterate over the Collection along with having the add & remove functionality.
- It helps to access the element using the index.
- It has the power of both, IEnumrable & ICollection interface.
IDictionary: This helps to iterate over the Collection with a key-value pair and also provides with add & remove functionality.
Difference between IEnumrable & IQueryable
- IQueryable inherits from IEnumrable.
- IQueryable executes the query on server side with all filters whereas IEnumrable executes the query and then filters the records.
- IQueryable is suitable for out-memory operations whereas IEnumrable is suitable for in-memory operations, such as dataset, ArrayList.
- IQueryable does not support further filtering whereas IEnumrable supports further filtering of data.
- IQueryable supports lazing loading and is best suitable for paging whereas IEnumrable doesn’t, as it executes the query with all the filters.
- IQueryable supports custom query whereas IEnumrable doesn’t support custom query.
- IQueryable is derived from System.LINQ whereas IEnumrable derives from System.Collection.