C#如何遍历Dictionary

这篇文章主要为大家详细介绍了C#遍历Dictionary的方法,.NET中的Dictionary是键/值对的集合,使用起来比较方便,Dictionary也可以用KeyValuePair来迭代遍历,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了C#如何遍历Dictionary的具体代码,供大家参考,具体内容如下

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace _02DictionaryIterator { class Program { static void Main(string[] args) { Console.WriteLine("开始遍历集合"); IteratorDictionary(); Console.ReadKey(); } ///  /// 遍历Dictionary集合 ///  private static void IteratorDictionary() { Dictionary dictionary = new Dictionary { {"张三", 1}, {"李四", 2}, {"王五", 3}, {"赵六", 4}, {"田七", 5} }; foreach (KeyValuePair keyValuePair in dictionary) { Console.WriteLine("key:{0}\tvalue:{1}", keyValuePair.Key, keyValuePair.Value); } } } } 

效果图:

以上就是本文的全部内容,教会大家C#遍历Dictionary的方法,谢谢大家的阅读。

以上就是C#如何遍历Dictionary的详细内容,更多请关注0133技术站其它相关文章!

赞(0) 打赏
未经允许不得转载:0133技术站首页 » 其他教程