Collection in C#

What are Collections?

Collections are nothing but a grouping of items into a single unit. Before we start to go to the office, we are looking at our collection, that is “Bag." Bag is a collection of items like laptop (if company provided), Lunch box, Pen, ID card, Zandu balm, Vicks inhaler.

Now the question that arises in our mind is Generic and Non Generic collections.

The collection of different items into a single unit is called Non Generic collections. So we conclude that our Bag is a good example of Non Generic collection, since it contains different items as we mentioned above.
The collection of similar items into a single unit is called Generic Collections.

So we conclude that Footwear rack is the example for Generic collections, since it contains only chapples, shoes and slippers, I hope no one can put a  lunch box in to Footwear rack.

Now we think in programming view, here generic collections are nothing but the collections of same types (like int, double, long). The generic collections are specific to the type of data we are going to store in it. If we have to be clear about type of data, then generic collections is the preferable choice.

C# is loaded with different type of collections, but here I am taking a single collection for explaining, that is Stack, the following program shows how our Generic Stack is going to hold the similar type of data.
  1. namespace GenericAndNonGeneric  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Stack < int > GenericCollection = new Stack < int > ();  
  8.             GenericCollection.Push(1);  
  9.             GenericCollection.Push(2);  
  10.             GenericCollection.Push(3);  
  11.             GenericCollection.Push(4);  
  12.             GenericCollection.Push(5);  
  13.             Console.WriteLine("The Contents of the Generic Collection are:");  
  14.             foreach(int i in GenericCollection)  
  15.             {  
  16.                 Console.WriteLine(i);  
  17.             }  
  18.             Console.ReadLine();  
  19.         }  
  20.     }  
  21. }  
In the above code, our collection “GenericCollection” is storing only integer values, if you try to store something else apart from int type, it results in compilation error.

The generic collections are available in namespace called System.Collections.Generic, need to specify data type in < >.

Example for Generic Collections

Array, Dictionary, List

Non generic collections are the collections of the different types, like storing different types of data as a single unit, in the below program we see Stack collection as a non generic collection.
  1. namespace GenericAndNonGeneric  
  2. {  
  3.     class Program  
  4.     {  
  5.         static void Main(string[] args)  
  6.         {  
  7.             Stack NonGenericCollection = new Stack();  
  8.             NonGenericCollection.Push(1);  
  9.             NonGenericCollection.Push("C# coding");  
  10.             object o = "123Vivek";  
  11.             NonGenericCollection.Push(o);  
  12.             NonGenericCollection.Push("#@@@@*&&&");  
  13.             NonGenericCollection.Push(55.67);  
  14.             Console.WriteLine("The Contents of the Non Generic Collection are:");  
  15.             foreach(object obj in NonGenericCollection)  
  16.             {  
  17.                 Console.WriteLine(obj);  
  18.             }  
  19.             Console.ReadLine();  
  20.         }  
  21.     }  
  22. }  
In the above code we are putting different types of data (int,string,object,double) into a “NonGenericCollection”. The Non Generic collections are available in namespace called System.Collections
 
Example for Non Generic Collections:

ArrayList, Hash Table.

Now we have to look at line of code “foreach(object obj in NonGenericCollection)” in above example, in this line we are converting all our entered data into a Object type, since we need a common holding point to hold different types of data, so we are using object type.

Performance Issue

Generic collections are faster than Non Generic collections, since type casting (Boxing)  happens in the case of non generic collections.

Conclusion

Based on the requirement we have to decide the usage of collections, either Generic or Non Generic.

Some of the collections like Stack and Queue are used as both Generic and Non Generic collections.

Guys please put comments, it will help me to improve. 
X

Build smarter apps with Machine Learning, Bots, Cognitive Services - Start free.

Start Learning Now