Search
Submit & Win
Submit and win
Cash Prizes !
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Interviews Author
Sun, 21-Dec-2014 Authors
All Time Authors
Vishal@Neeraj
43920
Rajkatie
20680
Bharathi Cherukuri
18040

Latest members | More ...


(Statistics delayed by 5 minutes)
 Exclusive Interview Question

C# and .NET interview questions - What are the different types of generic collections?

Posted on 5/17/2011 | Category: C# Interview questions | Views: 7257




There are basically four different types of generic collections which are as follows:-


1. List:-Lists are indexed based Generic Collections. Lists are Generic form of ArrayList.


List helps us to create flexible strong type collection as you can see in below code snippet i have defined List as "int" and "string".

//index based Generic collection            

List<int> ObjInt = new List<int>();        
ObjInt.Add(123);           
ObjInt.Add(456);           
Console.WriteLine(ObjInt[0]); //accessing the List by internal index based value.           
List<string> ObjString = new List<string>();           
ObjString.Add("feroz");

2. Dictionary:-Dictionary are key based generics collection.
                     Dictionary are generic form of Hashtable.

 //key based Generic collection            

Dictionary<int, int> ObjDict = new Dictionary<int,int>();           
ObjDict.Add(1,2);           
Dictionary<int, string> ObjDict1 = new Dictionary<int,string>();           
ObjDict1.Add(3, "feroz is a developer");           
ObjDict1.Add(4, "wasim is a developer");           
Console.WriteLine(ObjDict1[3]); //accessing the dictionary by defined key.

3. Stack:-Stack generic collection allows you to get value in "LIFO"(last in first out) manner.

// Stack             

Stack<string> ObjStack = new Stack<string>();           
ObjStack.Push("feroz");           
ObjStack.Push("moosa");           
Console.WriteLine(ObjStack.Pop());

4. Queue:-Queue generic collection allows you to get value in "FIFO"(first in first out) manner.

//Queue            

Queue<int> ObjStr = new Queue<int>();           
ObjStr.Enqueue(789);           
ObjStr.Enqueue(456);           
Console.WriteLine(ObjStr.Dequeue());

Genericollection.jpg


 


For more information about generic, please watch the below video.


Asked In: Many Interviews Alert Moderator 

Share this to:

Bookmark It

Comments or Responses

Posted by: Akiii on: 5/17/2011

Good article

Regards
Akiii

Login to post response