Use foreach over the dictionary itself: foreach (var (key, value) in dict). Each item is a KeyValuePair<TKey, TValue>, so the older form foreach (var pair in dict) with pair.Key and pair.Value works too. To change a value, assign through the indexer: dict[key] = newValue.
Dictionary<TKey, TValue> is enumerable, so a plain foreach visits every entry, and its Keys and Values properties let you loop over just one side. This page covers each of those, why looping over the keys and indexing back into the dictionary costs more than it looks, how to update values (including the increment-a-counter case), what you may and may not change inside the loop, the order you get entries in, and how to merge one dictionary into another. Each example runs on this page: hit Run, then edit the code and run it again.
1foreach over KeyValuePairRecommended
Enumerating a Dictionary yields one KeyValuePair<TKey, TValue> per entry, with Key and Value properties. Write the type out, or let var infer it. Since .NET Core 2.0, KeyValuePair also has a Deconstruct method, so foreach (var (key, value) in dict) gives you two named variables straight away. It is an ordinary foreach, so break, continue, return and await all behave as usual.
var stock = new Dictionary<string, int> { ["apple"] = 12, ["banana"] = 0, ["cherry"] = 7, }; // The canonical loop: one KeyValuePair<TKey, TValue> per entry. foreach (KeyValuePair<string, int> pair in stock) { Console.WriteLine($"{pair.Key} -> {pair.Value}"); } // var infers KeyValuePair<string, int>; break and continue work as usual. int units = 0; foreach (var pair in stock) { if (pair.Value == 0) continue; units += pair.Value; } Console.WriteLine($"units: {units}"); // .NET Core 2.0+: deconstruct the pair into two named variables. foreach (var (fruit, count) in stock) { Console.WriteLine($"{fruit,-7}{count,3}"); } // A KeyValuePair prints as [key, value], so string.Join gives a quick dump. Console.WriteLine(stock.First()); Console.WriteLine(string.Join(", ", stock));
Output
Prints apple -> 12, banana -> 0, cherry -> 7, then units: 19, then the deconstructed loop as an aligned table ({fruit,-7} pads the name, {count,3} right-aligns the number). stock.First() prints [apple, 12], and string.Join gives [apple, 12], [banana, 0], [cherry, 7]. These entries come out in the order they were added because nothing has been removed yet, but Dictionary doesn't promise any order; section 5 shows when it changes. The deconstruction form needs .NET Core 2.0 or later; on .NET Framework, use pair.Key and pair.Value.
2Keys and Values, and why Keys + dict[key] is wasteful
When you need only one side, loop over dict.Keys or dict.Values. Both are live views of the dictionary, not copies, and both work with LINQ (stock.Values.Sum()). The pattern to avoid is looping over Keys and reading dict[key] inside: every indexer call hashes the key and searches for the entry the loop was already standing on. The snippet passes a comparer that counts hash calls to make the difference visible.
// A comparer that counts hash lookups, so we can see what each loop costs. var comparer = new CountingComparer(); var stock = new Dictionary<string, int>(comparer) { ["apple"] = 12, ["banana"] = 0, ["cherry"] = 7, }; // Keys only, values only: both are live views, not copies. foreach (string fruit in stock.Keys) { Console.WriteLine($"fruit: {fruit}"); } Console.WriteLine($"units: {stock.Values.Sum()}, values: [{string.Join(", ", stock.Values)}]"); // The wasteful pattern: walk the keys, then look each one up again. comparer.Lookups = 0; int total = 0; foreach (string fruit in stock.Keys) { total += stock[fruit]; } Console.WriteLine($"Keys + stock[key]: total {total}, lookups {comparer.Lookups}"); // Enumerating the pairs hands you the value with no lookup at all. comparer.Lookups = 0; total = 0; foreach (var (_, count) in stock) { total += count; } Console.WriteLine($"foreach pair: total {total}, lookups {comparer.Lookups}"); // One key you are not sure exists: TryGetValue is a single lookup. comparer.Lookups = 0; if (stock.TryGetValue("cherry", out int cherries)) { Console.WriteLine($"cherry: {cherries}, lookups {comparer.Lookups}"); } class CountingComparer : IEqualityComparer<string> { public int Lookups; public bool Equals(string? x, string? y) => string.Equals(x, y); public int GetHashCode(string s) { Lookups++; return s.GetHashCode(); } }
Output
The keys print as fruit: apple, fruit: banana, fruit: cherry, then units: 19, values: [12, 0, 7]. Both totals are 19, but the lookup counts differ: Keys + stock[key]: total 19, lookups 3 against foreach pair: total 19, lookups 0. The _ discards the key in the deconstruction when you only want the value. For a single key that may be missing, TryGetValue does one lookup (cherry: 7, lookups 1), where ContainsKey followed by the indexer does two.
3Update a value in a dictionary
dict[key] = value is the update: it overwrites the value if the key exists and adds the key if it doesn't. Add is stricter and throws on a duplicate key, and TryAdd (.NET Core 2.0+) returns false instead. The common trap is counts[word]++ on a key that isn't there yet, because the read half of ++ throws. GetValueOrDefault fixes that, and on .NET 6+ CollectionsMarshal.GetValueRefOrAddDefault does it with one lookup instead of two.
using System.Runtime.InteropServices; var prices = new Dictionary<string, decimal> { ["tea"] = 2.50m, ["coffee"] = 3.00m }; // The indexer setter adds a missing key or overwrites an existing one. prices["coffee"] = 3.20m; prices["juice"] = 4.00m; Console.WriteLine(string.Join(", ", prices)); // Add() refuses a key that is already there; TryAdd() returns false instead. try { prices.Add("tea", 9.99m); } catch (ArgumentException e) { Console.WriteLine($"{e.GetType().Name}: {e.Message}"); } Console.WriteLine($"TryAdd tea: {prices.TryAdd("tea", 9.99m)}, tea is still {prices["tea"]}"); // Counting words: ++ on a missing key throws, so read with a default first. string[] words = ["to", "be", "or", "not", "to", "be"]; var counts = new Dictionary<string, int>(); try { counts["to"]++; } catch (KeyNotFoundException e) { Console.WriteLine($"{e.GetType().Name}: {e.Message}"); } foreach (string word in words) { counts[word] = counts.GetValueOrDefault(word) + 1; } Console.WriteLine(string.Join(", ", counts)); // .NET 6+: one lookup that adds the key if needed and returns a ref to the value. var fast = new Dictionary<string, int>(); foreach (string word in words) { ref int slot = ref CollectionsMarshal.GetValueRefOrAddDefault(fast, word, out _); slot++; } Console.WriteLine(string.Join(", ", fast)); // Updating every value inside the loop: current .NET allows overwriting existing keys. foreach (var (item, price) in prices) { prices[item] = Math.Round(price * 1.1m, 2); } Console.WriteLine(string.Join(", ", prices)); // Code that must also run on .NET Framework loops over a copy of the keys. foreach (string item in prices.Keys.ToList()) { prices[item] -= 0.25m; } Console.WriteLine(string.Join(", ", prices));
Output
The indexer gives [tea, 2.50], [coffee, 3.20], [juice, 4.00]. Add throws ArgumentException: An item with the same key has already been added. Key: tea, and TryAdd prints TryAdd tea: False, tea is still 2.50. The bare ++ throws KeyNotFoundException: The given key 'to' was not present in the dictionary., and both counting loops print [to, 2], [be, 2], [or, 1], [not, 1]. The last two loops change every price: [tea, 2.75], [coffee, 3.52], [juice, 4.40], then [tea, 2.50], [coffee, 3.27], [juice, 4.15]. Overwriting an existing key inside a foreach works on current .NET, but the documentation only promises that Remove and Clear leave the enumerator valid, and on .NET Framework any change throws. Looping over Keys.ToList() is the version that is safe everywhere. You can't assign pair.Value: KeyValuePair is read-only, so it won't compile.
4Removing entries while iterating
Since .NET Core 3.0, Dictionary<TKey, TValue>.Remove inside a foreach over the same dictionary is allowed. Adding a new key is not: the next step of the loop throws InvalidOperationException. If your code also targets .NET Framework, where Remove in the loop throws too, collect the keys first and remove them afterwards, or build a filtered copy with LINQ.
var scores = new Dictionary<string, int> { ["alice"] = 90, ["bob"] = 45, ["carol"] = 72, ["dana"] = 30, }; // .NET Core 3.0+: Remove() inside the foreach is allowed for Dictionary<TKey, TValue>. var passed = new Dictionary<string, int>(scores); foreach (var (name, score) in passed) { if (score < 50) passed.Remove(name); } Console.WriteLine($"remove in loop: {string.Join(", ", passed)}"); // Adding a key during the loop still breaks the enumerator. var broken = new Dictionary<string, int>(scores); try { foreach (var (name, score) in broken) { broken[name + "-retake"] = score + 10; } } catch (InvalidOperationException e) { Console.WriteLine($"{e.GetType().Name}: {e.Message}"); } Console.WriteLine($"entries now: {broken.Count}"); // Works everywhere: collect the keys first, then remove them. var kept = new Dictionary<string, int>(scores); var failing = kept.Where(pair => pair.Value < 50).Select(pair => pair.Key).ToList(); foreach (string name in failing) { kept.Remove(name); } Console.WriteLine($"collect, remove: {string.Join(", ", kept)} (dropped {string.Join(", ", failing)})"); // Or build a new dictionary with only the entries you want. var filtered = scores.Where(pair => pair.Value >= 50).ToDictionary(); Console.WriteLine($"ToDictionary(): {string.Join(", ", filtered)}");
Output
Removing in the loop prints remove in loop: [alice, 90], [carol, 72]. Adding a key throws InvalidOperationException: Collection was modified; enumeration operation may not execute., and entries now: 5 shows the worse part: the first new key went in before the exception, so the dictionary is left half-changed. Collecting the keys first prints collect, remove: [alice, 90], [carol, 72] (dropped bob, dana), and the filtered copy is ToDictionary(): [alice, 90], [carol, 72]. The parameterless ToDictionary() on a sequence of pairs is .NET 8+; on older versions write .ToDictionary(pair => pair.Key, pair => pair.Value).
5Iteration order: Dictionary, SortedDictionary, OrderedDictionary
The documentation says the order of a Dictionary is undefined. In practice a dictionary that has only ever had keys added enumerates in insertion order, which is why so much code quietly depends on it, until a Remove frees a slot and the next new key fills the gap. When order matters, sort while iterating with OrderBy, or pick a type that defines one: SortedDictionary keeps keys sorted, and .NET 9 added the generic OrderedDictionary<TKey, TValue> for insertion order with indexes.
var ports = new Dictionary<string, int> { ["http"] = 80, ["https"] = 443, ["ssh"] = 22, }; Console.WriteLine($"Dictionary: {string.Join(", ", ports.Keys)}"); // Order is undefined: after a Remove, the next new key can take the freed slot. ports.Remove("http"); ports["dns"] = 53; Console.WriteLine($"after Remove, Add: {string.Join(", ", ports.Keys)}"); // Need an order? Sort while you iterate (the dictionary itself is unchanged)... foreach (var (name, port) in ports.OrderBy(pair => pair.Key)) { Console.WriteLine($" {name} = {port}"); } Console.WriteLine($"by value, desc: {string.Join(", ", ports.OrderByDescending(pair => pair.Value))}"); // ...or use a collection that keeps its keys sorted all the time. var sorted = new SortedDictionary<string, int>(ports); sorted["ftp"] = 21; Console.WriteLine($"SortedDictionary: {string.Join(", ", sorted.Keys)}"); // .NET 9+: OrderedDictionary<TKey, TValue> keeps insertion order and has indexes. var ordered = new OrderedDictionary<string, int> { ["https"] = 443, ["ssh"] = 22 }; ordered.Remove("https"); ordered["dns"] = 53; ordered.Insert(0, "ftp", 21); Console.WriteLine($"OrderedDictionary: {string.Join(", ", ordered.Keys)}; first is {ordered.GetAt(0)}");
Output
Prints Dictionary: http, https, ssh, then after Remove, Add: dns, https, ssh: dns was added last but comes out first, in the slot http left. The OrderBy loop prints dns = 53, https = 443, ssh = 22, and sorting by value gives [https, 443], [dns, 53], [ssh, 22]. The SortedDictionary puts the new key in its place: dns, ftp, https, ssh. The OrderedDictionary keeps the order you built: ftp, ssh, dns, with GetAt(0) returning [ftp, 21]. Before .NET 9, the only OrderedDictionary was the non-generic one in System.Collections.Specialized, which stores object keys and values.
6Merge two dictionaries
There is no built-in Merge, so merging is a loop over the second dictionary. What decides the code is the rule for keys that appear in both: the indexer lets the second one win, TryAdd keeps the first, and Concat + ToDictionary throws. For any other rule, or more than two dictionaries, flatten them with SelectMany and GroupBy the key.
var defaults = new Dictionary<string, string> { ["theme"] = "light", ["lang"] = "en", ["font"] = "mono" }; var user = new Dictionary<string, string> { ["theme"] = "dark", ["zoom"] = "125%" }; // Last one wins: copy the first, then let the indexer overwrite from the second. var settings = new Dictionary<string, string>(defaults); foreach (var (key, value) in user) { settings[key] = value; } Console.WriteLine($"indexer: {string.Join(", ", settings)}"); // First one wins: TryAdd skips keys that are already there. var keepFirst = new Dictionary<string, string>(defaults); foreach (var (key, value) in user) { keepFirst.TryAdd(key, value); } Console.WriteLine($"TryAdd: {string.Join(", ", keepFirst)}"); // Concat + ToDictionary is one line, but it throws on a duplicate key. try { var merged = defaults.Concat(user).ToDictionary(); } catch (ArgumentException e) { Console.WriteLine($"{e.GetType().Name}: {e.Message}"); } // Any number of dictionaries, with your own rule for duplicates (here: add them up). Dictionary<string, int>[] sales = [ new() { ["north"] = 10, ["south"] = 4 }, new() { ["south"] = 6, ["east"] = 3 }, new() { ["north"] = 1 }, ]; var totals = sales .SelectMany(dict => dict) .GroupBy(pair => pair.Key) .ToDictionary(group => group.Key, group => group.Sum(pair => pair.Value)); Console.WriteLine($"summed: {string.Join(", ", totals)}");
Output
The indexer merge prints indexer: [theme, dark], [lang, en], [font, mono], [zoom, 125%], and the TryAdd merge keeps [theme, light] with the same other keys. Concat + ToDictionary throws ArgumentException: An item with the same key has already been added. Key: theme, so use it only when the keys can't overlap. The grouped version sums duplicates: summed: [north, 11], [south, 10], [east, 3]. Copying with new Dictionary<string, string>(defaults) first leaves both inputs unchanged; drop the copy to merge into defaults in place.
7Which should you use?
| Method | Gives you | Lookups per entry | Best for |
|---|---|---|---|
| foreach (var (k, v) in dict) | Key and value | None | The default loop, .NET Core 2.0+ |
| foreach (var pair in dict) | KeyValuePair | None | Any version, including .NET Framework |
| foreach (var k in dict.Keys) | Keys only | None | When you never need the value |
| dict.Values | Values only | None | Sums, counts, scans over values |
| Keys + dict[key] | Key and value | One extra | Avoid: loop over the pairs instead |
| dict.OrderBy(p => p.Key) | A defined order | None, plus a sort | Printing, reports, stable output |
| Keys.ToList() / Where(…).ToList() | A copy to loop over | One per change | Changing the dictionary on any .NET version |
Frequently asked questions
What is the best way to iterate over a dictionary in C#?
A foreach over the dictionary itself: foreach (var (key, value) in dict) on .NET Core 2.0+, or foreach (var pair in dict) with pair.Key and pair.Value on any version. Each entry is visited once and its value comes with it. If you only need one side, loop over dict.Keys or dict.Values. Avoid looping over Keys and reading dict[key] inside, which does an extra hash lookup per entry.
How do I update a value in a Dictionary?
Assign through the indexer: dict[key] = newValue overwrites the value, or adds the key if it is missing. For counters, counts[key]++ throws KeyNotFoundException when the key is new, so write counts[key] = counts.GetValueOrDefault(key) + 1. You cannot assign pair.Value inside a foreach, because KeyValuePair is read-only: the compiler reports error CS0200, "Property or indexer 'KeyValuePair<string, int>.Value' cannot be assigned to -- it is read only".
Why do I get "Collection was modified; enumeration operation may not execute"?
Because the dictionary changed in a way its enumerator does not allow while the foreach was running. On .NET Core 3.0 and later, Remove and Clear are allowed during the loop, and overwriting an existing key works on current .NET, but adding a new key throws InvalidOperationException. On .NET Framework every change throws. The fix that works everywhere is to loop over a copy, such as foreach (var key in dict.Keys.ToList()), or to collect the keys to change first and apply the changes after the loop.
Does a C# Dictionary keep insertion order?
Not by contract. The documentation leaves the order undefined. A dictionary that has only had keys added usually enumerates in insertion order, but after a Remove the next new key can fill the freed slot and come out earlier. Use dict.OrderBy(p => p.Key) when you need a sorted walk, SortedDictionary<TKey, TValue> to keep keys sorted all the time, or OrderedDictionary<TKey, TValue> (.NET 9+) to keep insertion order.
How do I merge two dictionaries in C#?
Loop over the second one and assign with the indexer, foreach (var (k, v) in second) first[k] = v;, so duplicate keys take the second value. Use first.TryAdd(k, v) in the loop to keep the first value instead. first.Concat(second).ToDictionary() throws ArgumentException on a duplicate key. For a one-liner where the last value wins, group first: a.Concat(b).GroupBy(p => p.Key).ToDictionary(g => g.Key, g => g.Last().Value).
How do I get a dictionary entry by index?
dict.ElementAt(1) returns the second KeyValuePair the enumerator yields, such as [b, 2], but it walks the entries to get there and the order is undefined, so it is rarely what you want. If you need positions, use OrderedDictionary<TKey, TValue> on .NET 9+: GetAt(index) returns the pair at that position and IndexOf(key) returns a key's position.
Why does Console.WriteLine(dict) print the type name instead of the contents?
Dictionary does not override ToString(), so you get its full type name, which starts with System.Collections.Generic.Dictionary and ends with [System.String,System.Int32] for a Dictionary<string, int>. Each KeyValuePair does format itself as [key, value], so string.Join(", ", dict) prints the whole dictionary as [a, 1], [b, 2], [c, 3].