Is there a trick in creating a generic list of anonymous type?

前端 未结 9 1697
有刺的猬
有刺的猬 2021-01-30 05:25

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

        
9条回答
  •  无人及你
    2021-01-30 05:52

    It's worth to note that finally, there is possibility to use such syntax. It had been introduced in C# 7.0

    var tanks = new List<(Tank AttackingTank, Tank TargetTank)>();
    
    (Tank, Tank) tTank = (new Tank(), new Tank());
    tanks.Add(tTank);
    
    var a = tanks[0].AttackingTank;
    var b = tanks[0].TargetTank;
    

提交回复
热议问题