C# 三元运算符调用函数

aCu*_*ria 4 c# ternary-operator

为什么此代码无效?很确定它在 C /C++ 中是合法的

伪代码:

String s = Console.ReadLine();
int x = 0;
Int32.TryParse(s, out x) ? Console.WriteLine("Foo") :  Console.WriteLine("bar");
Run Code Online (Sandbox Code Playgroud)

Sal*_*ari 5

三元运算符用于返回值,并且必须分配这些值。

如果要在三元运算符中调用 void 方法,可以使用如下委托:

String s = Console.ReadLine();
int x = 0;
(Int32.TryParse(s, out x) ? new Action(() => Console.WriteLine("Foo")) : () => Console.WriteLine("bar"))();
Run Code Online (Sandbox Code Playgroud)