Sometimes i need to use a Tuple, for example i have list of tanks and their target tanks (they chase after them or something like that ) :
List
Here's a hack:
var myList = Enumerable.Empty()
.Select(dummy => new { AttackingTank = default(Tank), TargetTank = default(Tank), })
.ToList();
If Tank
is a class type, you can write (Tank)null
instead of default(Tank)
. You can also use some Tank
instance you happen to have at hand.
EDIT:
Or:
var myList = Enumerable.Repeat(
new { AttackingTank = default(Tank), TargetTank = default(Tank), },
0).ToList();
If you make a generic method, you won't have to use Enumerable.Empty
. It could go like this:
static List GetEmptyListWithAnonType(TAnon dummyParameter)
{
return new List();
}
It is to be called with the TAnon
inferred from usage, of course, as in:
var myList = GetEmptyListWithAnonType(new { AttackingTank = default(Tank), TargetTank = default(Tank), });