Collection in C# with Example
Collections in C# are data structures that are used to store, manage, and manipulate groups of objects. They are an integral part of the .NET Framework, providing a more flexible way to work with groups of objects. Unlike arrays, most collections are dynamic, meaning they can grow and shrink in size dynamically. This article explores the different types of collections in C# and provides examples of how to use them.
What is Collection in C#
A collection in C# is a class designed to hold and manage objects. The System.Collections and System.Collections.Generic namespaces provide classes that manage collections of objects. Collections can dynamically expand as needed and provide various ways to manipulate the data they contain.
Types of Collection in C# with Example
C# supports several different types of collections, each designed for specific situations:
1. List (Generic Collection) A List is a generic collection that stores elements in a linear order. It allows elements to be accessed by an index and is one of the most commonly used collections.
List<string> fruits = new List<string>();
fruits.Add("Apple");
fruits.Add("Banana");
fruits.Add("Cherry");
Console.WriteLine(fruits[1]); // Outputs "Banana"
2. Dictionary (Generic Collection) A Dictionary in C# is a collection of key/value pairs that are stored based on the hash code of the key.
Dictionary<int, string> employeeDict = new Dictionary<int, string>();
employeeDict.Add(1, "John Doe");
employeeDict.Add(2, "Jane Doe");
Console.WriteLine(employeeDict[1]); // Outputs "John Doe"
3. Queue (Non-Generic Collection) A Queue represents a first in, first out (FIFO) collection of objects.
Queue myQueue = new Queue();
myQueue.Enqueue("Hello");
myQueue.Enqueue("World");
Console.WriteLine(myQueue.Dequeue()); // Outputs "Hello"
4. Stack (Non-Generic Collection) A Stack represents a last in, first out (LIFO) collection of objects.
Stack myStack = new Stack();
myStack.Push("Hello");
myStack.Push("World");
Console.WriteLine(myStack.Pop()); // Outputs "World"
What is Non-Generic Collection in C#
Non-generic collections can store items of any type and are not type-safe. They are found in the System.Collections namespace. Examples include ArrayList, Hashtable, Queue, and Stack.
ArrayList myArrayList = new ArrayList();
myArrayList.Add(1);
myArrayList.Add("Two");
myArrayList.Add(3.0);
Collection in C# with Example Interview Questions
When preparing for interviews, understanding collections is crucial. Here are some examples of interview questions:
1. What are the advantages of using a Dictionary over a List?
- Dictionaries allow faster lookups based on keys, while Lists are better for sequential access.
2. How would you remove an item from a List?
- You can remove an item from a List by calling the Remove or RemoveAt method.
List<string> cities = new List<string> { "New York", "London", "Mumbai" };
cities.Remove("London"); // Removes "London" from the list
3. Explain the difference between a Queue and a Stack.
- A Queue is a FIFO data structure, where the first element added is the first to be removed. A Stack is a LIFO data structure, where the last element added is the first to be removed.
Collections are a fundamental concept in C# and understanding them is crucial for developing effective applications that manage groups of objects efficiently.