System.Collections Namespace in C#
What is the System.Collections Namespace in C#?
The System.Collections namespace in C# provides a set of collection classes that allow storing, retrieving, and manipulating objects efficiently. These collections store data in various structures such as lists, queues, stacks, and dictionaries.
Key Features of System.Collections
- Provides dynamic data structures like lists, queues, stacks, and dictionaries.
- Supports non-generic collections (useful before generics were introduced).
- Stores elements as
object
type, requiring type casting. - Supports operations like sorting, searching, and modifying collections.
Common Collection Classes in System.Collections
Below are some commonly used collection classes from the System.Collections
namespace:
Collection Type | Description | Example Use Case |
---|---|---|
ArrayList |
Dynamic array that can grow or shrink. | Storing a list of student names dynamically. |
Stack |
Last-In-First-Out (LIFO) collection. | Implementing undo/redo functionality. |
Queue |
First-In-First-Out (FIFO) collection. | Managing a printer queue. |
Hashtable |
Stores key-value pairs with fast lookups. | Storing configuration settings. |
SortedList |
Stores key-value pairs in sorted order. | Keeping track of employees sorted by ID. |
Example: Using ArrayList
The ArrayList
class dynamically resizes as elements are added.
Example: Working with ArrayList
using System;
using System.Collections;
class Program
{
static void Main()
{
ArrayList list = new ArrayList();
list.Add("Alice");
list.Add("Bob");
list.Add("Charlie");
Console.WriteLine("ArrayList elements:");
foreach (var item in list)
{
Console.WriteLine(item);
}
list.Remove("Bob");
Console.WriteLine("After removing 'Bob':");
foreach (var item in list)
{
Console.WriteLine(item);
}
}
}
// Output:
// ArrayList elements:
// Alice
// Bob
// Charlie
// After removing 'Bob':
// Alice
// Charlie
The ArrayList
class dynamically resizes as elements are added or removed.
Example: Using Stack
The Stack
class follows the Last-In-First-Out (LIFO) principle.
Example: Implementing Stack
using System;
using System.Collections;
class Program
{
static void Main()
{
Stack stack = new Stack();
stack.Push("Page 1");
stack.Push("Page 2");
stack.Push("Page 3");
Console.WriteLine($"Popped Item: {stack.Pop()}");
Console.WriteLine($"Top Item: {stack.Peek()}");
}
}
// Output:
// Popped Item: Page 3
// Top Item: Page 2
The Stack
class allows pushing and popping elements in LIFO order.
Example: Using Queue
The Queue
class follows the First-In-First-Out (FIFO) principle.
Example: Implementing Queue
using System;
using System.Collections;
class Program
{
static void Main()
{
Queue queue = new Queue();
queue.Enqueue("Task 1");
queue.Enqueue("Task 2");
queue.Enqueue("Task 3");
Console.WriteLine($"Dequeued Item: {queue.Dequeue()}");
Console.WriteLine($"Next Item: {queue.Peek()}");
}
}
// Output:
// Dequeued Item: Task 1
// Next Item: Task 2
The Queue
class processes elements in FIFO order.
Best Practices for Using System.Collections
- Prefer generic collections (e.g.,
List<T>
) over non-generic collections for better type safety. - Use
Hashtable
for fast key-based lookups when generic dictionaries are not an option. - Choose
Queue
for processing tasks in FIFO order. - Use
Stack
when handling LIFO operations like undo/redo functionality.