Skip to content
Justice Tech School

Dictionary

Dictionary<K, V> stores key value pairs. Use TryGetValue to read safely.

Example
var d = new Dictionary<string, int> { ["a"] = 1, ["b"] = 2 };
d["c"] = 3;
if (d.TryGetValue("b", out var v)) Console.WriteLine(v);
foreach (var (k, val) in d) Console.Write($"{k}={val} ");
Console.WriteLine(d.ContainsKey("z"));

Exercise

Safe read.

d.("k", out var v)

Quiz Test

What if a key is missing with d["x"]?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.