【发布时间】:2015-10-01 05:24:52
【问题描述】:
我想将 Dictionary 的参数传递给方法。
public static void Method(Dictionary<string, string> hash)
我真的很想使用内联集合初始化器
这是不合法的
Method({{"Foo", "Bar"}, {"Bing", "Bong"}})
这也不是
Method(new {{"Foo", "Bar"}, {"Bing", "Bong"}})
这是
// outside the class
using D = System.Collections.Generic.Dictionary<string, string>;
// calling with the type alias
Method(new D {{"Foo", "Bar"}, {"Bing", "Bong"}})
原来如此
// once in a library somewhere
class SD : System.Collections.Generic.Dictionary<string, string> {}
// calling with the class
Method(new SD {{"Foo", "Bar"}, {"Bing", "Bong"}})
你有像我不知道的这样一个很好的捷径来解决我的类型集合初始化吗?
【问题讨论】:
-
我宁愿清晰而不是简洁(或者更糟糕的是,使用可以作为速记的语句),输入它有什么问题?
-
@ronBeyer 输入没问题。只是问是否完成
can,而不是它should。 -
stackoverflow.com/questions/1815252/… 是另一种方式,但它几乎与
new Dictionary<string, string> { ... }一样长。 -
不需要匿名作为值...