What is the neatest / shortest way I can write an inline collection initializer?
I dont care about reference names, indexes are fine, and the item only needs to be used in the scope of the method.
I think an anonymous type collection would be messier because I would have to keep writing the key name every time.
I've currently got
var foo = new Tuple<int, string, bool>[] 
{ 
   new Tuple<int, string, bool>(1, "x", true), 
   new Tuple<int, string, bool>(2, "y", false) 
};
Im hoping c# 4.0 will have something ive missed.
The shortest you can get is to use Tuple.Create instead of new Tuple:
var foo = new [] { Tuple.Create(1, "x", true), Tuple.Create(2, "y", false) };
                        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