使用三元运算符进行C#数组初始化?

时间:2013-10-24 23:20:08

标签: c# .net ternary-operator

它们似乎不适用于C#

string[] directions = 
      IsSomeValueTrue ? string[]{"RIGHT", "LEFT"} : string[]{"UP", "DOWN"};

string[] directions = 
      IsSomeValueTrue ? new string[]{"RIGHT", "LEFT"} : new string[]{"UP", "DOWN"};

我缺少什么?

2 个答案:

答案 0 :(得分:3)

最短的单行:

var dirs = IsSomeValueTrue ? new[] {"RIGHT", "LEFT"} : new[] {"UP", "DOWN"};

答案 1 :(得分:0)

试试这个:

string[] directions = IsSomeValueTrue ? new string[] {"RIGHT", "LEFT"} : new string[]{"UP", "DOWN"};