In the previous discussion we had learnt about the different types of Collections in the .NET framework, how are they differentiated and in which cases they are used. We also discussed that which one is most preferably used. We then discussed about the concept of Generics and Generic Constraints.

Now, continuing the discussion further as there were some topics which were not discussed in the previous part. In this part we will be discussing about the Generic Collections and also its types and some other concepts.

Now we have to remember here one thing that Generics and Generic Collections are totally different concepts. Nothing is common in them, so make sure that we don’t confuse ourselves.

Generic Collections:

Now as we have seen that the function of generics was to decouple the datatype from the main logic for reusability. So by using the function of generics a new type of collections was emerged or created. That new type of collection we call it as Generic Collections.

As we know that some kind of logic is separated or decoupled from the datatype. Here the question arises what kind of logic it is? Here collection itself is a logic which we are separating it from the datatype. Now this collection logic contains functions like Add, Delete, Search, etc.

Also now the question comes that where these generic collections are used? Now as we see that the developers or we can say that people who have created the .Net, what they have done is that all the normal and regular .NET collections, they have converted into generic collections by applying the generic logic to all of them. For e.g. the ArrayList collection in .NET has become a generic collection known as List, also the Hashtable collection in .NET has become a generic collection known as Dictionary. Now remember that the normal .NET collections are there and they are not changed, they are used according to their functionalities. We are only applying the generic logic to these collections to make them generic collections.

They come under the namespace of Using System.Collections.Generic;

So in short if we want to use index based generic collection we use List, if we want to use Key value pair based generic collection we use Dictionary and if we want to use the prioritized based generic collection we use Stack Generics and Queue Generics.

To understand this properly we will see some small sample code examples.

For index based generic collection:

class Program
{
     static void Main(string[] args)
           {
                List<int> obj = new List<int>();
                 obj. Add(1234);
          }
}

For Key Value Pair Based generic collection:

class Program

{

     static void Main(string[] args)

           {

Dictionary<int, int> objDictionary = new Dictionary<int, int>();

                 objDictionary.Add(7, 123);

            }

}

For Stack & Queue based or prioritized based generic collection:

class Program

{

     static void Main(string[] args)

           {

                Stack<int> ostack = new Stack<int>();

                Ostack.Push(1);   

                Ostack.Pop(1);

               Queue<int> Qobj = new Queue<int>();

               Qobj.Enqueue(1);

               Qobj.Dequeue(1);

           }

}

So to conclude Generic collections are faster as there is no boxing & unboxing happening and they are strong typed. And every normal .NET collection has a generic form.

Interfaces in Generic Collections:

Now we will discuss about interfaces of the generic collection. What are interface in generic collection? Where are they used? What are their functionalities? We will be discussing the answers to these questions and many other questions to understand the concept properly.

So now there are 4 types of generic collection interfaces, they are:

  • IEnumerable
  • ICollection
  • IDictionary
  • IList

There is a letter“I” in front of all these names which definitely means that they are interfaces and due to which we can assume that all the functions of interfaces like decoupling, polymorphism can be used. Also these generic interfaces help us to implement the Object Oriented Programming concepts such as Encapsulation, Polymorphism, etc.

So Encapsulation concept of Object Oriented Programming can be implemented by using the generic collection interface, where we can control how much access of collection is to be given to the end user.

Also Polymorphism concept of Object Oriented Programming can be implemented by generic collection interface, where we can dynamically point to any collections on runtime.

Now we will see how these interfaces implement the concept of encapsulation.

IEnumerable:

This interface is used when we don’t want the user side or the user to change any data from the collection. And therefore we use IEnumerable as its function is to allow the user to only browse through the collection and nothing else the user can do apart from browsing.

ICollection:

This interface is used when we want to browse the collection along with the count property. The count property lets us know that what is the count of the collection like 10,100, 1000, etc. Therefore the main function of the ICollection is to browse the collection with the count property.

IList & IDictionary:

Now these two interfaces are used when we want to browse the collection with count property and also here we have the authority to make changes in the collection. Changes like add, remove, etc. Therefore the functions of these interfaces IList & IDictionary is to browse count and also make the changes in the collection. It means that these interfaces have the full permission to browse through the collections and can make changes in them. Here IList is for ArrayList and IDictionary is for Hashtables.

So in this way we can implement the object oriented programming concept of encapsulation by using these interfaces and controlling the access to collections which should be given to the end user.

Now for the concept of polymorphism to be implemented using interfaces we see how it works out for these interfaces like IEnumerable, IList, etc.

Here we can consider the IEnumerable as the parent class because it has only the browse function. Now suppose we write a code for IList and also a code for IDictionary and in the output we keep some option saying that if that option is true it is IList or otherwise if it is false it is IDictionary. Now if we do not want to make our code complicated by changing the code to IList and IDictionary wherever necessary we use IEnumerable instead because we have considered it as parent class.

So now we can understand that the parent class can point towards any child classes during runtime, in the same way the IEnumerable can point towards any interface like IList or IDictionary during runtime. So by the concept of polymorphism the object in the code can behave differently under different conditions.

So in short we can conclude that the interfaces are very much helpful in sending the collections from one layer to other layer in a 3 layered architecture by controlling how much access is to be given to the user end. For e.g. if the user end is an only a reading screen the IEnumerable is used and if the user end is as data entry screen then IList or IDictionary is used.

As we have seen the implementation of Encapsulation and Polymorphism through the interfaces generic collections, there are some questions which needed to be answered.

Now there are many differences in between the interfaces, but here we will discuss the important ones which create the confusion most of the time.

Difference between IEnumerable & IEnumerator:

Firstly we will see that the functionality of IEnumerable & IEnumerator which is one and the same for both of them, i.e. they both are used to browse the collections.

Now the question comes that if the functionality of both these interfaces are same then is there any difference?

The difference between them is that IEnumerable is a sugar coating or we can say that it is a syntactic sugar. It means that the code is small and simple compared to the code in IEnumerator which is a bit lengthy and confusing. So now we will obviously assume that to preferably use IEnumerable instead of IEnumerator as their functions are same. IEnumerable internally uses the same logic as of the IEnumerator for browsing the collections.

But this was only the small difference, the major difference is that whenever we split the collection data in to some parts to read or browse them sequentially it happens that IEnumerator remembers it cursor position and will continue from that position onwards. Whereas in IEnumerable it does not remember its cursor position and can start to browse the data from anywhere. We will take a simple example to understand this,

In a collection there is some data like

1A

2B

3C

4D

5E

6F

Now we are splitting the collection saying that it will first browse from 1A to 3C and from 4D to 6F. Now in IEnumerator it happens that when the cursor has read the data 3C, it remembers its position and from next it will directly start from 4D.

But in IEnumerable when the data 3C is read after that it does not remember its cursor position, so what happens is that when we have to browse the other part of collection which is from 4D to 6F it will not directly start from 4D but it will start from anywhere may be from the beginning also.

So therefore this is the major difference between IEnumerable & IEnumerator.

Difference between IEnumerable & IQueryable:

As we have seen the functions of IEnumerable to browse the collection and to provide a sugar coating code to the IEnumerator to reduce complexity. Now we have to remember one important thing that IQueryable interface of generic collection is inherited from IEnumerable. So whatever the IEnumerable interface can do all that same things IQueryable can also do.

But there are many differences between them, but here we will discuss one difference which is huge and also important.

Now when we apply filter on a collection both IEnumerable & IQueryable work in different ways.

Now what IEnumerable does is that as the filter is applied on the collection it goes to the database browse the collection, fetches the collection and gives it to the client side. Now at the client side the filter is applied on the collection. Due to this the client gets the filtered values along with the other unnecessary values.

Now what the IQueryable does is that it goes to the database browses the collection and before fetching it to the client side it applies filter on the collection and take the filtered values and fetch it and gives to the client side. Now the client side will receive only the filtered data and not any unnecessary data.

We can say that this is the main difference between the IEnumerable & IQueryable. If we want the filtered data and our collection data is coming from in memory application then we can use IEnumerable, but if our collection data is coming from the database then we have to use IQueryable.

The difference between IEnumerable & IQueryable comes into picture, where we can see that how filter logic is applied. Oe executes on the client side and other executes on the database side.

We can take a simple example where we apply filter logic on a collection where in the output we want StudentID. So in IEnumerable the collection database is browsed and fetched it to the client side where it will apply filter logic and give StudentID output along with all the other values from the collection.

And in IQueryable the collection database browse the collection from database and apply the filter logic in the database itself and then taking the required output of StudentID and giving it to the client side and not giving any other values from the collection.

So this is the major difference between IEnumerable & IQueryable.

 

Advertisements