Deconstruction in foreach over Dictionary

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.");
}

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)