我在 foreach
中有以下 if else 语句环形:
string date = GetDate(item)
if (date == null)
{
continue;
}
else
{
article.Date = date;
}
我想用 ? :
以简洁的格式写出来。 :string date = GetDate(item)
date == null ? continue : article.Date = date;
来自 what I gather ,这应该可以工作,因为它的格式是 condition ? first_expression : second_expression;
, 其中 first_expression
是 continue
,但是在 Visual Studio 2015 中,我看到给定区域的这些错误:继续
Invalid expression term 'continue'
Syntax error, ':' expected
Invalid expression term 'continue'
; expected
:
; expected
: expected
是否可以使用
continue
在这种类型的If/Else
?如果没有,这有什么理由吗?
最佳答案
https://msdn.microsoft.com/en-gb/library/ms173144.aspx
An expression is a sequence of one or more operands and zero or more operators that can be evaluated to a single value, object, method, or namespace
continue
不是表达式您的代码正在尝试分配
continue
到您的“日期”变量。这没有意义。恐怕没有办法使用三元运算符来完成您要完成的工作。
关于c# - Continue as First Statement in Concise If Else,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38696150/