Iterating over an IDictionary interface
In
C#, it's possible to iterate over key/value pairs with
foreach … in
.
This is demonstrated in the following example: The method
GetEnvironmentVariables()
method of
System.Environment
returns the set of
environment variables as an
IDictionary
. The
foreach
loop iterates over this set and prints the name of the environment variable (
Key
) and its value (
Value
).
using System;
using System.Collections;
static class Prg {
public static void Main() {
IDictionary envVars = Environment.GetEnvironmentVariables();
foreach (DictionaryEntry envVar in envVars) {
Console.WriteLine("{0,-40}: {1}", envVar.Key, envVar.Value);
}
}
}