c# iterate dictionary by sorted keys code example
Example 1: iterate through dictionary c#
foreach(var item in myDictionary)
{
foo(item.Key);
bar(item.Value);
}
Example 2: c# iterate sortedDictionary
using System;
using System.Collections.Generic;
class SortedDictionaryEnumerationDemo
{
static void Main()
{
var dict = new SortedDictionary<int, string>();
dict.Add(4, "Four");
dict.Add(5, "Five");
dict.Add(1, "One");
dict.Add(3, "Three");
dict.Add(2, "Two");
Console.WriteLine("== Enumerating Items ==");
foreach (var item in dict)
{
Console.WriteLine("{0} => {1}", item.Key, item.Value);
}
Console.WriteLine("\n== Enumerating Keys ==");
foreach (int key in dict.Keys)
{
Console.WriteLine("{0} => {1}", key, dict[key]);
}
Console.WriteLine("\n== Enumerating Values ==");
foreach (string value in dict.Values)
{
Console.WriteLine("{0} => {1}", value, GetKeyFromValue(dict, value));
}
}
static int GetKeyFromValue(SortedDictionary<int, string> dict, string value)
{
try
{
return
(from item in dict
where item.Value.Equals(value)
select item.Key).First();
}
catch (InvalidOperationException e)
{
return -1;
}
}
}