Convert a C# string array to a dictionary

前端 未结 7 1851
死守一世寂寞
死守一世寂寞 2021-01-07 06:20

Is there an elegant way of converting this string array:

string[] a = new[] {\"name\", \"Fred\", \"colour\", \"green\", \"sport\", \"tennis\"};
7条回答
  •  灰色年华
    2021-01-07 06:43

    Since it's an array I would do this:

    var result = Enumerable.Range(0,a.Length/2)
                           .ToDictionary(x => a[2 * x], x => a[2 * x + 1]);
    

提交回复
热议问题