テストの目的でIEnumerable<KeyValuePair<string, string>>
、次のサンプルキーと値のペアを使用してオブジェクトを作成する必要があります。
Key = Name | Value : John
Key = City | Value : NY
これを行うための最も簡単なアプローチは何ですか?
テストの目的でIEnumerable<KeyValuePair<string, string>>
、次のサンプルキーと値のペアを使用してオブジェクトを作成する必要があります。
Key = Name | Value : John
Key = City | Value : NY
これを行うための最も簡単なアプローチは何ですか?
のいずれか:
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);
Dictionary<string, string>
を実装しIEnumerable<KeyValuePair<string,string>>
ます。
var List = new List<KeyValuePair<String, String>> {
new KeyValuePair<String, String>("Name", "John"),
new KeyValuePair<String, String>("City" , "NY")
};
Dictionary<string,string> testDict = new Dictionary<string,string>(2);
testDict.Add("Name","John");
testDict.Add("City","NY");
それはあなたが意味することですか、それともそれ以上のものがありますか?
にを割り当てるだけDictionary<K, V>
ですIEnumerable<KeyValuePair<K, V>>
IEnumerable<KeyValuePair<string, string>> kvp = new Dictionary<string, string>();
それがうまくいかない場合は、試すことができます-
IDictionary<string, string> dictionary = new Dictionary<string, string>();
IEnumerable<KeyValuePair<string, string>> kvp = dictionary.Select((pair) => pair);