In this article, we’ll explore how to enumerate dictionary keys while working with a collection of collections. To make it engaging, let's take a new example where we manage a collection of universities categorized by countries. Each country is a key in a dictionary, and the value is a list of universities in that country.
Key Concepts
-
Dictionary Keys Enumeration:
- A dictionary allows us to store data as key-value pairs. The
Keys
property is used to enumerate all the keys in the dictionary.
- A dictionary allows us to store data as key-value pairs. The
-
Checking Key Existence:
- The
ContainsKey
method ensures that we don’t access a key that doesn’t exist in the dictionary.
- The
-
Accessing and Processing Values:
- The value corresponding to a key is accessed using square bracket syntax, which in this example, is a list of universities.
-
LINQ for Filtering Results:
- LINQ provides methods like
Take
to process collections effectively.
- LINQ provides methods like
Full Code Example
using System;
using System.Collections.Generic;
using System.Linq;
class Program
{
static void Main()
{
// Dictionary of countries and their respective universities
var universities = new Dictionary<string, List<University>>
{
{
"USA",
new List<University>
{
new University("MIT", "Massachusetts"),
new University("Stanford University", "California"),
new University("Harvard University", "Massachusetts"),
// Add more universities if needed
}
},
{
"UK",
new List<University>
{
new University("University of Oxford", "Oxford"),
new University("University of Cambridge", "Cambridge"),
new University("Imperial College London", "London"),
// Add more universities if needed
}
}
};
// Display available countries
Console.WriteLine("Available countries:");
foreach (var country in universities.Keys)
{
Console.WriteLine(country);
}
// Ask the user to select a country
Console.WriteLine("\nEnter a country:");
string selectedCountry = Console.ReadLine();
// Check if the selected country exists in the dictionary
if (universities.ContainsKey(selectedCountry))
{
Console.WriteLine($"\nTop universities in {selectedCountry}:");
// Retrieve and display universities in the selected country
foreach (var university in universities[selectedCountry].Take(5))
{
Console.WriteLine($"- {university.Name}, located in {university.Location}");
}
}
else
{
Console.WriteLine("Country not found.");
}
}
}
// University class definition
class University
{
public string Name { get; set; }
public string Location { get; set; }
public University(string name, string location)
{
Name = name;
Location = location;
}
}
Explanation of the Code
-
Defining the Dictionary:
- A dictionary named
universities
maps country names (string
) to a list ofUniversity
objects.
- A dictionary named
-
Enumerating Keys:
- The
Keys
property is used to list all available countries. This makes it easy for the user to select a country.
- The
-
User Input Validation:
- The
ContainsKey
method ensures that the program doesn’t crash when an invalid country is entered.
- The
-
Displaying Results:
- For the selected country, the top universities (up to 5) are displayed using the
Take
method from LINQ.
- For the selected country, the top universities (up to 5) are displayed using the
Sample Output
Input Scenario 1: Valid Country
Available countries:
USA
UK
Enter a country:
USA
Top universities in USA:
- MIT, located in Massachusetts
- Stanford University, located in California
- Harvard University, located in Massachusetts
Input Scenario 2: Invalid Country
Available countries:
USA
UK
Enter a country:
Canada
Country not found.
Key Takeaways
-
Dictionary Keys Enumeration: Use the
Keys
property to access all keys in a dictionary. -
Key Validation: Always validate user input with
ContainsKey
. -
LINQ for Filtering: Methods like
Take
make it easy to limit and process collections.
Top comments (0)