C#/.NET/.NET Core应用程序编程开发中循环迭代一个字典的每一项目的方法有哪些呢?
4 个回答
-
C# 7.0新增了新的解构(Deconstruction)语法,所以,我们可以使用解构语法可循环迭代字典,如下:
var dic = new Dictionary<int, string>() { { 1, "One" }, { 2, "Two" }, { 3, "Three" } }; foreach (var (key, value) in dic) { Console.WriteLine($"Item [{key}] = {value}"); } foreach (var (_, value) in dic) { Console.WriteLine($"Item [NO_ID] = {value}"); } foreach ((int key, string value) in dic) { Console.WriteLine($"Item [{key}] = {value}"); }