Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shortest inline collection initializer? C#

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.

like image 839
maxp Avatar asked Aug 23 '11 09:08

maxp


1 Answers

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) };
like image 108
Daniel Hilgarth Avatar answered Oct 11 '22 21:10

Daniel Hilgarth