If you don’t like having to write the Deconstruct
method, especially if you only need it in one place, here’s how to do it as a one-liner with LINQ:
Using your original dictionary:
var dic = new Dictionary<string, int>{ ["Bob"] = 32, ["Alice"] = 17 };
You can do it like this:
foreach (var (name, age) in dic.Select(x => (x.Key, x.Value)))
{
Console.WriteLine($"{name} is {age} years old.");
}