Can I use a collection initializer for Dictionary entries?

前端 未结 7 1330
猫巷女王i
猫巷女王i 2020-12-09 14:19

I want to use a collection initializer for the next bit of code:

public Dictionary GetNames()
{
    Dictionary names =          


        
7条回答
  •  时光说笑
    2020-12-09 14:52

    In the following code example, a Dictionary is initialized with instances of type StudentName.

      Dictionary students = new Dictionary()
      {
          { 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}}
      };
    

    from msdn

提交回复
热议问题