I am currently initializing a Hashtable in the following way:
Hashtable filter = new Hashtable(); filter.Add("building", "A-51"); filter.Add("apartment", "210");
I am looking for a nicer way to do this.
I tried something like
Hashtable filter2 = new Hashtable() { {"building", "A-51"}, {"apartment", "210"} };
However the above code does not compile.
The exact code you posted:
Hashtable filter2 = new Hashtable() { {"building", "A-51"}, {"apartment", "210"} };
Compiles perfectly in C# 3. Given you reported compilation problems, I'm guessing you are using C# 2? In this case you can at least do this:
Hashtable filter2 = new Hashtable(); filter2["building"] = "A-51"; filter2["apartment"] = "210";
In C# 3 it should compile fine like this:
Hashtable table = new Hashtable {{1, 1}, {2, 2}};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With