What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 16937 |  Welcome, Guest!   Register  Login
 Home > Interview Questions > Exclusive Interview Questions > Exclusive C# Interview Questions > C# and .NET interview questions - What a ...

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

Category: C# | Difficulty Level: Beginners | Views:4764



Answer:

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.


Get Questpond's 500+ .NET Interview preparation videos at discounted price

Found interesting? Add this to:


Page copy protected against web site content infringement by Copyscape

 Responses

Posted by: Akiii | Posted on: 17 May 2011 11:47:38 PM

Good article

Regards
Akiii

>> Write Response - Respond to this post and get points

More ...

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/21/2013 7:26:03 PM