C# / C# Collections and Generics
Exploring Generic Collections
In this tutorial, you will explore the use of Generic Collections in C#. These versatile data structures can store any data type, providing both flexibility and type safety.
Section overview
5 resourcesIntroduces collections and generics to handle and manipulate data efficiently.
1. Introduction
In this tutorial, we'll explore the functionality and versatility of Generic Collections in C#. Generics allow you to define type-safe data structures, meaning that you can check for the data type at compile time, preventing common type mismatches.
By the end of this tutorial, you'll learn:
- What Generic Collections are and why they're useful
- How to use List
- Best practices for working with Generic Collections
Prerequisites: Basic knowledge of C# programming language is required.
2. Step-by-Step Guide
Understanding Generic Collections
Generic collections are classes provided by the .NET Framework to store, retrieve, and manipulate data in a type-safe manner. The most commonly used Generic Collections are List
Using List
List
List<int> numbers = new List<int>();
numbers.Add(1);
numbers.Add(2);
numbers.Add(3);
In this example, we create a list of integers and add three elements to it.
3. Code Examples
Example 1: Using List
Let's explore how to use the List
List<int> numbers = new List<int> {1, 2, 3, 4, 5}; // Initialize the list with some numbers
numbers.Add(6); // Add an item to the end of the list
numbers.Insert(0, 0); // Insert an item at a specific position
foreach (int number in numbers) // Traverse the list
{
Console.WriteLine(number); // Output: 0, 1, 2, 3, 4, 5, 6
}
Example 2: Using Dictionary
A Dictionary
Dictionary<string, int> ages = new Dictionary<string, int>(); // Create a new dictionary
ages.Add("John", 28); // Add a key-value pair
ages.Add("Jane", 30); // Add another key-value pair
foreach (KeyValuePair<string, int> entry in ages) // Traverse the dictionary
{
Console.WriteLine($"Name: {entry.Key}, Age: {entry.Value}"); // Output: Name: John, Age: 28 and Name: Jane, Age: 30
}
4. Summary
In this tutorial, we've learned about Generic Collections in C#, why they're useful, and how to use List
For further learning, consider reading Microsoft's official documentation on Generic Collections.
5. Practice Exercises
- Exercise 1: Create a List
of your favorite movies and print them out. - Exercise 2: Create a Dictionary
that stores student names and their grades. Print out each student's name and grade.
Solutions:
- Solution 1:
List<string> movies = new List<string> {"Inception", "The Dark Knight", "Interstellar"};
foreach (string movie in movies)
{
Console.WriteLine(movie);
}
- Solution 2:
Dictionary<string, string> grades = new Dictionary<string, string> {{"John", "A"}, {"Jane", "B"}};
foreach (KeyValuePair<string, string> entry in grades)
{
Console.WriteLine($"Name: {entry.Key}, Grade: {entry.Value}");
}
For further practice, consider creating and manipulating a Stack
Need Help Implementing This?
We build custom systems, plugins, and scalable infrastructure.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI in Public Safety: Predictive Policing and Crime Prevention
In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…
Read articleAI in Mental Health: Assisting with Therapy and Diagnostics
In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…
Read articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article