是否有针对 OrderedDictionary 键数组的单行解决方案?

我正在寻找一种单行解决方案来获取 OrderedDictionary 的键。我已经通过使用以下方法获得了一个可行的解决方案,但是有没有一种方法可以使用更少的代码来通过使用 LINQ 来实现相同的目标?


private string[] GetKeys(OrderedDictionary orderedDictionary)

{

    List<string> keyList = new List<string>(orderedDictionary.Count);


    foreach (DictionaryEntry entry in orderedDictionary)

    {

        keyList.Add(entry.Key.ToString());

    }


    return keyList.ToArray();

}



慕神8447489
浏览 116回答 2
2回答

翻过高山走不出你

您可以使用它来减少代码 -var&nbsp;myKeys&nbsp;=&nbsp;orderedDictionary.Keys.Cast<string>().ToArray();

慕森卡

Code-Apprentice 是对的,string[] myKeys = new string[orderedDictionary.Count];orderedDictionary.Keys.CopyTo(myKeys, 0);
打开App,查看更多内容
随时随地看视频慕课网APP