Collections
Dictionary Lookup
A dictionary maps keys to values for fast lookup by name.
dictionary
A dictionary stores key-value pairs.
Dictionary Lookup
DictionaryLookup.cs
Replay: real traced execution (multi-file project)
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string key = "go";
Dictionary<string, int> scores = new Dictionary<string, int>
{
["csharp"] = 94,
["go"] = 88
};
bool found = scores.ContainsKey(key);
int value = found ? scores[key] : 0;
Console.WriteLine($"key={key}");
Console.WriteLine($"found={found}");
Console.WriteLine($"value={value}");
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string key = "csharp";
Dictionary<string, int> scores = new Dictionary<string, int>
{
["csharp"] = 94,
["go"] = 88
};
bool found = scores.ContainsKey(key);
int value = found ? scores[key] : 0;
Console.WriteLine($"key={key}");
Console.WriteLine($"found={found}");
Console.WriteLine($"value={value}");
}
}
using System;
using System.Collections.Generic;
class Program
{
static void Main()
{
string key = "ruby";
Dictionary<string, int> scores = new Dictionary<string, int>
{
["csharp"] = 94,
["go"] = 88
};
bool found = scores.ContainsKey(key);
int value = found ? scores[key] : 0;
Console.WriteLine($"key={key}");
Console.WriteLine($"found={found}");
Console.WriteLine($"value={value}");
}
}
key ← go, scores ← System.Collections.Generic.Dictionary`2[System.String,System.Int32]
5{6 static void Main()7 {8 string key→ go = "go"; //@key="csharp", "ruby"9 Dictionary<string, int> scores→ System.Collections.Generic.Dictionary`2[System.String,System.Int32] = new Dictionary<string, int>10 {11 ["csharp"] = 94,12 ["go"] = 8813 };14 bool found→ True = scoresSystem.Collections.Generic.Dictionary`2[System.String,System.Int32].ContainsKey(keygo);15 int value→ 88 = foundTrue ? scores[key]88 : 0;1617 Console.WriteLine($"key={keygo}");18 Console.WriteLine($"found={foundTrue}");19 Console.WriteLine($"value={value88}");20 }outputkey=go found=True value=88
key ← csharp, scores ← System.Collections.Generic.Dictionary`2[System.String,System.Int32]
5{6 static void Main()7 {8 string key→ csharp = "csharp";9 Dictionary<string, int> scores→ System.Collections.Generic.Dictionary`2[System.String,System.Int32] = new Dictionary<string, int>10 {11 ["csharp"] = 94,12 ["go"] = 8813 };14 bool found→ True = scoresSystem.Collections.Generic.Dictionary`2[System.String,System.Int32].ContainsKey(keycsharp);15 int value→ 94 = foundTrue ? scores[key]94 : 0;1617 Console.WriteLine($"key={keycsharp}");18 Console.WriteLine($"found={foundTrue}");19 Console.WriteLine($"value={value94}");20 }outputkey=csharp found=True value=94
key ← go, scores ← System.Collections.Generic.Dictionary`2[System.String,System.Int32]
5{6 static void Main()7 {8 string key→ go = "go"; //@key="csharp", "ruby"9 Dictionary<string, int> scores→ System.Collections.Generic.Dictionary`2[System.String,System.Int32] = new Dictionary<string, int>10 {11 ["csharp"] = 94,12 ["go"] = 8813 };14 bool found→ True = scoresSystem.Collections.Generic.Dictionary`2[System.String,System.Int32].ContainsKey(keygo);15 int value→ 88 = foundTrue ? scores[key]88 : 0;1617 Console.WriteLine($"key={keygo}");18 Console.WriteLine($"found={foundTrue}");19 Console.WriteLine($"value={value88}");20 }outputkey=go found=True value=88
Follow the Lookup
- The dictionary starts with scores for
csharpandgo. - The selected
keyis"go". ContainsKey(key)checks whether that key exists.- When the key is found,
scores[key]reads the matching value. - When the key is missing, the fallback value is
0. | Key | Found? | Value used | | --- | --- | --- | |go| true |88| |ruby| false |0|
Exercise: DictionaryLookup.cs
Look up a score by key, print whether it was found, and use 0 when the key is missing