Collections in C#

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
 
 
  1. Arrays are strongly typed entities.
  2. 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.
  3. It’s a sequential collection which will be of the same type.
  4. Elements can be accessed by using the index of an element.
  5. Each index is initialized with a default value depending on the type of array created.
Working with ArrayList
  1. It is a sophisticated version of an array.
  2. It is one of the generic collection types present in System.Collection.Generic namespace.
  3. An ArrayList can be used to create a collection of any type, such as String, Int16 etc., and even complex types.
  4. The object stored in the list can be accessed by an Index.
  5. Unlike arrays, List can grow its size automatically
  6. List has methods to search, sort, and manipulate the list.
  7. It uses both,the equality comparer as well as the ordering comparer.
  8. List class is a strongly typed class.
Difference between Array and ArrayList
  1. Arrays are used when the collection is of fixed length.
  2. Array is faster than ArrayList because array doesn't require boxing / un-boxing.
  3. Arrays are always strongly typed as compared to ArrayList.
  4. ArrayList can grow automatically.
Working With Hashtable
 
 
  1. It is a collection of Key-value pairs that are organized on the hash code of the key.
  2. It uses key to link the elements in the Collection.
  3. Key can be of any type.
  4. Key cannot be null.
  5. Value against a key can be null.
  6. It is not strongly typed.
  7. Strong type of Hashtable is called Dictionary.


Difference between ArrayList and Hashtable
  1. Hashtable uses the key to access a specific element in a Collection whereas the ArrayList uses index which is zero based.
  2. 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
  1. This helps to iterate over the Collection, without exposing the add, remove methods as in ArrayList, and only helps in browsing the Collection.
  2. It has a method called GetEnumrator which returns IEnumerator object that helps to iterate over Collection using foreach loop.
  3. Every .NET Collection implements IEnumerable interface.
ICollection
  1. This helps to iterate over the Collection, without exposing the add, remove methods as in ArrayList, and also helps in browsing the Collection.
  2. It is inherited from IEnumrable Interface.
  3. 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
  1. This helps to iterate over the Collection along with having the add & remove functionality.
  2. It helps to access the element using the index.
  3. 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
  1. IQueryable inherits from IEnumrable.
  2. IQueryable executes the query on server side with all filters whereas IEnumrable executes the query and then filters the records.
  3. IQueryable is suitable for out-memory operations whereas IEnumrable is suitable for in-memory operations, such as dataset, ArrayList.
  4. IQueryable does not support further filtering whereas IEnumrable supports further filtering of data.
  5. IQueryable supports lazing loading and is best suitable for paging whereas IEnumrable doesn’t, as it executes the query with all the filters.
  6. IQueryable supports custom query whereas IEnumrable doesn’t support custom query.
  7. IQueryable is derived from System.LINQ whereas IEnumrable derives from System.Collection.

Up Next
    Ebook Download
    View all
    Learn
    View all