What are different types of collections in .NET?
There are five important collections in .NET Arrays, Lists, Hashtable, stacks and queues.
What are hashtable collections?
In arraylist or array if we have to access any data we need to use the internal index id generated by the array list collection. For instance the below code snippet shows how the internal id is used to fetch data from array list.
In actual scenarios we hardly remember internal id’s generated by collection we would like to fetch the data by using some application defined key. There’s where hash table comes in to picture.
Hash table helps to locate data using keys as shown below. When we add data to hash table it also has a provision where we can add key with the data. This key will help us to fetch data later using key rather than using internal index id’s generated by collections.
This key is converted in to numeric hash value which is mapped with the key for quick lookup.
What are Queues and stack collection?
Queues are collection which helps us to add object and retrieve them in the manner they were added. In other word queues helps us to achieve the first in first out collection behavior.
Stack collection helps us to achieve first in last out behavior.
Can you explain the concept of generic collection?
Array List, Stack and Queues provide collections which are not type safe. This leads 2 problems first it’s not type safe and second it leads to boxing and unboxing.
By using generics we can have type safety and also we can avoid boxing and unboxing. Below is a simple code snippet which shows a strong typed list of type integer and string.
See following video on concurrent generic collections in C#: -
Click and see here for Generic Collections in .NET tutorials.