C# 14 · Collections and LINQ
Use a dictionary
Add and read values by key. TryGetValue avoids an exception for missing keys.
var ages = new Dictionary<string, int> { ["Ada"] = 36 };
if (ages.TryGetValue("Ada", out var age)) Console.WriteLine(age);
C# 14 · Collections and LINQ
Add and read values by key. TryGetValue avoids an exception for missing keys.
var ages = new Dictionary<string, int> { ["Ada"] = 36 };
if (ages.TryGetValue("Ada", out var age)) Console.WriteLine(age);
Was this page helpful?