我目前正在通过以下方式初始化Hashtable:
Hashtable filter = new Hashtable(); filter.Add("building", "A-51"); filter.Add("apartment", "210");
我正在寻找一种更好的方法来做到这一点.
我试过类似的东西
Hashtable filter2 = new Hashtable() { {"building", "A-51"}, {"apartment", "210"} };
但是上面的代码不能编译.
您发布的确切代码:
Hashtable filter2 = new Hashtable() { {"building", "A-51"}, {"apartment", "210"} };
在C#3中完美编译.鉴于你报告了编译问题,我猜你在使用C#2?在这种情况下,您至少可以这样做:
Hashtable filter2 = new Hashtable(); filter2["building"] = "A-51"; filter2["apartment"] = "210";
在C#3中它应该像这样编译好:
Hashtable table = new Hashtable {{1, 1}, {2, 2}};