打印字典值的简单方法?

use*_*815 2 c# dictionary

我有以下代码:

static void Main(string[] args)
{
    // Add 5 Employees to a Dictionary.
    var Employees = new Dictionary<int, Employee>();
    Employees.Add(1, new Employee(1, "John"));
    Employees.Add(2, new Employee(2, "Henry"));
    Employees.Add(3, new Employee(3, "Jason"));
    Employees.Add(4, new Employee(4, "Ron"));
    Employees.Add(5, new Employee(5, "Yan"));
}
Run Code Online (Sandbox Code Playgroud)

有没有一种简单的方法可以像 Java 一样简单地打印字典的值?例如,我希望能够打印如下内容:

拥有密钥 1 的员工:Id=1,姓名= John

拥有密钥 2 的员工:Id=2,姓名= Henry

.. ETC..

谢谢。

抱歉,我习惯了Java!

Rak*_*was 5

尝试使用foreach

foreach (var res in Employees)
{
    Console.WriteLine("Employee with key {0}: ID = {1}, Name = {2}", res.Key, res.Value.Id, res.Value.Name);
}
Run Code Online (Sandbox Code Playgroud)

或者,简单地使用 LINQ:

var output = String.Join(", ", Employees.Select(res => "Employee with key " + res.Key + ": ID = " + res.Value.Id + ", Name = " + res.Value.Name));
Run Code Online (Sandbox Code Playgroud)