Collections



In this section we will deal with C# collections. 



What is a collection?


The .NET framework provides specialized classes which gives more flexible way to work with groups of objects - this is collection.


Types of Collections in .NET:


There are 2 types of collection in .NET,


  1. Non Generic Collections.
  2. Generic Collections.


So, The difference between them are:

Generic
Non Generic
Generic collections are strongly typed.
Non Generic collections are not strongly typed, unless they are specifically written to accept just a single type of data.
Example: List<T>, Dictionary<TKey, TValue>, SortedList<TKey, TValue>, Hashset<T>, Queue<T>, Stack<T> etc.
Example: ArrayList, SortedList, Stack, Queue, Hashtable, BitArray etc.
Generic collection store elements internally in arrays of their actual types, so no boxing, unboxing or casting is required.
Non Generic collection store elements internally in ‘object’ arrays, which can store any types of data. That means in case of value types (ex: int, float, double etc) they have to be boxed first and then unboxed when you retrieve them.
You will not face this problem with reference types, but still you have to cast them to their actual type before you can use them.
As we don’t require boxing, unboxing or casting, so generic collections are much faster.
As it requires boxing, unboxing or casting, so they are slower than generic collections.

For more information on Generic collection visit: MSDN Link: System.Collection.Generic
For more information on Non Generic collection visit: MSDN Link: System.Collection



Comments

Popular posts from this blog

C# Tutorial