字典的可枚举或 Linq Foreach 循环

时间:2021-06-07 09:11:27

标签: c# dictionary foreach

这段代码用分隔符打印字典键和值。

public static void DictionaryPrint<T ,U >(IDictionary<T,U> dictionary,string separator = " ")
        {

            foreach (KeyValuePair<T, U> pair in dictionary)
            {
                Console.WriteLine(pair.Key + separator + pair.Value);
            }
        } 

问题:我可以写一些类似的东西

 public static void DictionaryPrint<T, U>(IDictionary<T, U> dictionary, string separator = " ")
       {
            // this is for example , i now that dictionary doesn't have built-in ForEach mathod 
        
            dictionary.ForEach(KeyValuePair<T,U> kvp => Console.WriteLine(kvp.Key + separator + kvp.Value)); 
       }

我需要类似 Java Stream Api 的东西。我试过 Linq 和 Enumerable 。

0 个答案:

没有答案