【发布时间】:2020-10-29 07:00:17
【问题描述】:
我正在学习条件运算符。 documentation 提到
condition ? consequent : alternative
在我的示例中,Test1() 是当前有效的方法,但我想使用 Test2() 中所示的条件运算符编写等效方法。
public void Test1(bool isTrue)
{
if (isTrue)
{
MethodA();
}
else
{
MethodB();
}
}
public void Test2(bool isTrue)
{
isTrue ? MethodA() : MethodB();
}
public void MethodA()
{
//Do This
}
public void MethodB()
{
//Do That
}
我收到 Test2() 的错误
"isTrue" 是一个变量,但用作类型
和
Local Function 'MethodA()' 必须声明一个主体,因为它没有标记为 'static extern'
有人能解释一下为什么会这样吗?
【问题讨论】: