Most elegant way to convert string array into a dictionary of strings

后端 未结 8 1028
没有蜡笔的小新
没有蜡笔的小新 2020-12-08 18:39

Is there a built-in function for converting a string array into a dictionary of strings or do you need to do a loop here?

8条回答
  •  清歌不尽
    2020-12-08 19:18

    You can use LINQ to do this, but the question that Andrew asks should be answered first (what are your keys and values):

    using System.Linq;
    
    string[] myArray = new[] { "A", "B", "C" };
    myArray.ToDictionary(key => key, value => value);
    

    The result is a dictionary like this:

    A -> A
    B -> B
    C -> C
    

提交回复
热议问题