Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to iterate any Map in C#
C# doesn't have a built-in Map type like Java. Instead, C# uses Dictionary<TKey, TValue> to achieve the same functionality. A Dictionary stores key-value pairs and provides several ways to iterate through its contents.
Syntax
Following is the syntax for creating a Dictionary −
Dictionary<TKey, TValue> dictionary = new Dictionary<TKey, TValue>();
Following are the common iteration patterns −
// Iterate over keys
foreach (var key in dictionary.Keys) { }
// Iterate over values
foreach (var value in dictionary.Values) { }
// Iterate over key-value pairs
foreach (var kvp in dictionary) { }
Using foreach with Keys
You can iterate through Dictionary keys and access corresponding values −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> d = new Dictionary<string, int>();
d.Add("keyboard", 1);
d.Add("mouse", 2);
d.Add("monitor", 3);
Console.WriteLine("Iterating through keys:");
foreach (var key in d.Keys) {
Console.WriteLine($"Key: {key}, Value: {d[key]}");
}
}
}
The output of the above code is −
Iterating through keys: Key: keyboard, Value: 1 Key: mouse, Value: 2 Key: monitor, Value: 3
Using foreach with Key-Value Pairs
The most efficient way to iterate through both keys and values is using KeyValuePair −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> d = new Dictionary<string, int>();
d["apple"] = 5;
d["banana"] = 3;
d["orange"] = 8;
Console.WriteLine("Iterating through key-value pairs:");
foreach (var kvp in d) {
Console.WriteLine($"Key: {kvp.Key}, Value: {kvp.Value}");
}
}
}
The output of the above code is −
Iterating through key-value pairs: Key: apple, Value: 5 Key: banana, Value: 3 Key: orange, Value: 8
Using foreach with Values Only
When you only need the values, iterate through the Values collection −
using System;
using System.Collections.Generic;
class Program {
static void Main() {
Dictionary<string, int> scores = new Dictionary<string, int>();
scores["Alice"] = 95;
scores["Bob"] = 87;
scores["Charlie"] = 92;
Console.WriteLine("Student scores:");
foreach (var score in scores.Values) {
Console.WriteLine($"Score: {score}");
}
}
}
The output of the above code is −
Student scores: Score: 95 Score: 87 Score: 92
Comparison of Iteration Methods
| Method | Performance | Use Case |
|---|---|---|
| foreach (var key in dict.Keys) | Good | When you need keys and occasionally access values |
| foreach (var kvp in dict) | Best | When you need both keys and values |
| foreach (var value in dict.Values) | Good | When you only need values |
Conclusion
In C#, use Dictionary<TKey, TValue> as the equivalent of a Map. The most efficient way to iterate is using foreach (var kvp in dictionary) when you need both keys and values. Choose the iteration method based on whether you need keys, values, or both.
