我想把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"}})你有没有一个我不知道的解决我的类型集合初始化的捷径?
发布于 2015-10-01 05:24:52
不,你不能用你想要的方式来声明一个字典。在C#6中,有一种稍微更漂亮的Dictionary初始化器语法,但它并不完全是您想要的:
using D = System.Collections.Generic.Dictionary<string, string>;
//...
Method(new D { ["Foo"] = "Bar", ["Bing"] = "Bong" });https://stackoverflow.com/questions/32875785
复制相似问题