创建了IEnumerable&LT; KeyValuePair&LT;字符串,字符串&GT;&GT;用C#对象? [英] Creating the IEnumerable<KeyValuePair<string, string>> Objects with C#?
本文介绍了创建了IEnumerable&LT; KeyValuePair&LT;字符串,字符串&GT;&GT;用C#对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
出于测试目的,我需要创建一个的IEnumerable&LT; KeyValuePair&LT;字符串,字符串&GT;&gt;在下面的示例键值对
目标:
For testing purposes, I need to create an IEnumerable<KeyValuePair<string, string>>
object with the following sample key value pairs:
Key = Name | Value : John
Key = City | Value : NY
什么是做到这一点的最简单的方法呢?
What is the easiest approach to do this?
推荐答案
任何的:
values = new Dictionary<string,string> { {"Name", "John"}, {"City", "NY"} };
或
values = new [] {
new KeyValuePair<string,string>("Name","John"),
new KeyValuePair<string,string>("City","NY")
};
或
values = (new[] {
new {Key = "Name", Value = "John"},
new {Key = "City", Value = "NY"}
}).ToDictionary(x => x.Key, x => x.Value);
这篇关于创建了IEnumerable&LT; KeyValuePair&LT;字符串,字符串&GT;&GT;用C#对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文