In the world of programming, managing data efficiently is crucial. C# provides a rich set of built-in collections that help developers organize and manipulate data effectively. In this tutorial, we will explore three fundamental generic collections in C#: List, Dictionary, and Queue. These collections are essential for handling various data structures and operations.
A List is an ordered collection of elements that can be accessed by index. It allows dynamic resizing as elements are added or removed. Lists are ideal when you need a flexible array-like structure with additional functionalities like sorting, searching, and more.
A Dictionary is a collection of key-value pairs where each key must be unique. Dictionaries provide fast access to data based on keys, making them highly efficient for scenarios requiring quick lookups.
A Queue follows the First-In-First-Out (FIFO) principle, meaning that the first element added to the queue is the first one to be removed. Queues are useful in scenarios where tasks need to be processed in the order they arrive, such as task scheduling or message queues.
Let's start by creating a List of integers and perform some basic operations:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8// Create a new List of integers9List<int> numbers = new List<int>();1011// Add elements to the list12numbers.Add(10);13numbers.Add(20);14numbers.Add(30);1516// Access elements by index17Console.WriteLine("First element: " + numbers[0]);1819// Iterate over the list20foreach (int number in numbers)21{22Console.WriteLine(number);23}2425// Remove an element26numbers.Remove(20);2728// Check if an element exists29bool contains = numbers.Contains(30);30Console.WriteLine("Contains 30: " + contains);31}32}
First element: 10 10 20 30 Contains 30: True
Next, let's create a Dictionary to store student names and their corresponding grades:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8// Create a new Dictionary with string keys and int values9Dictionary<string, int> grades = new Dictionary<string, int>();1011// Add key-value pairs to the dictionary12grades.Add("Alice", 95);13grades.Add("Bob", 88);14grades.Add("Charlie", 76);1516// Access a value by key17Console.WriteLine("Grade of Alice: " + grades["Alice"]);1819// Iterate over the dictionary20foreach (KeyValuePair<string, int> entry in grades)21{22Console.WriteLine(entry.Key + ": " + entry.Value);23}2425// Check if a key exists26bool hasBob = grades.ContainsKey("Bob");27Console.WriteLine("Has Bob: " + hasBob);2829// Remove a key-value pair30grades.Remove("Charlie");3132// Get the number of elements in the dictionary33int count = grades.Count;34Console.WriteLine("Number of students: " + count);35}36}
Grade of Alice: 95 Alice: 95 Bob: 88 Charlie: 76 Has Bob: True Number of students: 2
Finally, let's create a Queue to simulate a simple task processing system:
1using System;2using System.Collections.Generic;34class Program5{6static void Main()7{8// Create a new Queue of strings9Queue<string> tasks = new Queue<string>();1011// Enqueue tasks12tasks.Enqueue("Task 1");13tasks.Enqueue("Task 2");14tasks.Enqueue("Task 3");1516// Dequeue tasks and process them17while (tasks.Count > 0)18{19string task = tasks.Dequeue();20Console.WriteLine("Processing: " + task);21}2223// Check if the queue is empty24bool isEmpty = tasks.Count == 0;25Console.WriteLine("Queue is empty: " + isEmpty);26}27}
Processing: Task 1 Processing: Task 2 Processing: Task 3 Queue is empty: True
In the next section, we will dive deeper into the List collection in C#, exploring its methods and properties in more detail. Understanding how to effectively use lists will be crucial for many programming tasks.
Stay tuned!