Collections in C# Programming
C# provides a variety of collection types to store and manage groups of related objects. Commonly used collections include List, Dictionary, Queue, and Stack. These collections are part of the System.Collections.Generic namespace and offer flexibility, performance, and ease of use.
1. List
A List is a dynamic collection that allows duplicate elements and provides methods for adding, removing, and searching items.
Example: Using a List
using System; using System.Collections.Generic; class Program { static void Main() { // Create a List Listnumbers = new List (); // Add elements numbers.Add(10); numbers.Add(20); numbers.Add(30); // Access elements Console.WriteLine("First element: " + numbers[0]); // Iterate through the List Console.WriteLine("All elements:"); foreach (int num in numbers) { Console.WriteLine(num); } // Remove an element numbers.Remove(20); Console.WriteLine("After removal:"); foreach (int num in numbers) { Console.WriteLine(num); } } }
Output:
First element: 10
All elements:
10
20
30
After removal:
10
30
2. Dictionary
A Dictionary is a collection of key-value pairs. Each key must be unique, and it is used to retrieve the corresponding value.
Example: Using a Dictionary
using System; using System.Collections.Generic; class Program { static void Main() { // Create a Dictionary Dictionaryages = new Dictionary (); // Add key-value pairs ages["Alice"] = 25; ages["Bob"] = 30; ages["Charlie"] = 35; // Access values by key Console.WriteLine("Alice's age: " + ages["Alice"]); // Iterate through the Dictionary Console.WriteLine("All entries:"); foreach (var entry in ages) { Console.WriteLine(entry.Key + ": " + entry.Value); } } }
Output:
Alice's age: 25
All entries:
Alice: 25
Bob: 30
Charlie: 35
3. Queue
A Queue is a collection that follows the First-In-First-Out (FIFO) principle. Items are added at the end and removed from the front.
Example: Using a Queue
using System; using System.Collections.Generic; class Program { static void Main() { // Create a Queue Queuequeue = new Queue (); // Add elements queue.Enqueue("Alice"); queue.Enqueue("Bob"); queue.Enqueue("Charlie"); // Access and remove elements Console.WriteLine("Dequeued: " + queue.Dequeue()); // Iterate through the Queue Console.WriteLine("Remaining elements:"); foreach (string name in queue) { Console.WriteLine(name); } } }
Output:
Dequeued: Alice
Remaining elements:
Bob
Charlie
4. Stack
A Stack is a collection that follows the Last-In-First-Out (LIFO) principle. Items are added to and removed from the top.
Example: Using a Stack
using System; using System.Collections.Generic; class Program { static void Main() { // Create a Stack Stackstack = new Stack (); // Add elements stack.Push("Alice"); stack.Push("Bob"); stack.Push("Charlie"); // Access and remove elements Console.WriteLine("Popped: " + stack.Pop()); // Iterate through the Stack Console.WriteLine("Remaining elements:"); foreach (string name in stack) { Console.WriteLine(name); } } }
Output:
Popped: Charlie
Remaining elements:
Bob
Alice
Comparison of Collections
Collection | Order | Duplicates | Key-Value Pair |
---|---|---|---|
List | Maintains insertion order | Allows duplicates | No |
Dictionary | Keys unordered | Keys must be unique | Yes |
Queue | FIFO | Allows duplicates | No |
Stack | LIFO | Allows duplicates | No |
Conclusion
Collections in C# provide powerful tools for managing groups of objects. By understanding the characteristics and use cases of List, Dictionary, Queue, and Stack, you can select the appropriate collection for your application needs.