如何使用集合初始值设定项初始化字典(C# 编程指南)How to initialize a dictionary with a collection initializer (C# Programming Guide)

Dictionary<TKey,TValue> 包含键/值对集合。A Dictionary<TKey,TValue> contains a collection of key/value pairs. Add 方法采用两个参数,一个用于键,一个用于值。Its Add method takes two parameters, one for the key and one for the value. 若要初始化 Dictionary<TKey,TValue> 或其 Add 方法采用多个参数的任何集合,一种方法是将每组参数括在大括号中,如下面的示例中所示。One way to initialize a Dictionary<TKey,TValue>, or any collection whose Add method takes multiple parameters, is to enclose each set of parameters in braces as shown in the following example. 另一种方法是使用索引初始值设定项,如下面的示例所示。Another option is to use an index initializer, also shown in the following example.

示例Example

在下面的代码示例中,使用类型 StudentName 的实例初始化 Dictionary<TKey,TValue>In the following code example, a Dictionary<TKey,TValue> is initialized with instances of type StudentName. 第一个初始化使用具有两个参数的 Add 方法。The first initialization uses the Add method with two arguments. 编译器为每对 int 键和 StudentName 值生成对 Add 的调用。The compiler generates a call to Add for each of the pairs of int keys and StudentName values. 第二个初始化使用 Dictionary 类的公共读取/写入索引器方法:The second uses a public read / write indexer method of the Dictionary class:

public class HowToDictionaryInitializer
{
    class StudentName
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int ID { get; set; }
    }

    public static void Main()
    {
        var students = new Dictionary<int, StudentName>()
        {
            { 111, new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 } },
            { 112, new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } },
            { 113, new StudentName { FirstName="Andy", LastName="Ruth", ID=198 } }
        };

        foreach(var index in Enumerable.Range(111, 3))
        {
            Console.WriteLine($"Student {index} is {students[index].FirstName} {students[index].LastName}");
        }
        Console.WriteLine();		

        var students2 = new Dictionary<int, StudentName>()
        {
            [111] = new StudentName { FirstName="Sachin", LastName="Karnik", ID=211 },
            [112] = new StudentName { FirstName="Dina", LastName="Salimzianova", ID=317 } ,
            [113] = new StudentName { FirstName="Andy", LastName="Ruth", ID=198 }
        };

        foreach (var index in Enumerable.Range(111, 3))
        {
            Console.WriteLine($"Student {index} is {students2[index].FirstName} {students2[index].LastName}");
        }
    }
}

请注意,在第一个声明中,集合中的每个元素有两对大括号。Note the two pairs of braces in each element of the collection in the first declaration. 最内层的大括号中的是 StudentName 的对象初始值设定项,最外层的大括号中的是要添加到 students Dictionary<TKey,TValue> 的键/值对的初始值设定项。The innermost braces enclose the object initializer for the StudentName, and the outermost braces enclose the initializer for the key/value pair that will be added to the students Dictionary<TKey,TValue>. 最后,字典的整个集合初始值设定项被括在大括号中。Finally, the whole collection initializer for the dictionary is enclosed in braces. 在第二个初始化中,左侧赋值是键,右侧是将对象初始值设定项用于 StudentName 的值。In the second initialization, the left side of the assignment is the key and the right side is the value, using an object initializer for StudentName.

请参阅See also