c# - Continue as First Statement in Concise If Else

标签 c# .net if-statement visual-studio-2015

我在 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_expressioncontinue ,但是在 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/

相关文章:

java - 在 Java 中,添加 else 语句(在已使用的 if 之后)是否需要更长的执行时间?

c# - UI繁忙时更改光标

c# - 单击 CheckBoxList 上的项目时需要显示一些内容

c# - 如何使用 Azure.Storage v12 SDK 从 Azure Blob 中删除元数据?

python - swift if or/and 语句,如 python

javascript - 'else' 之后不必要的 'return' 。 (不返回)

c# - 如何添加更改其版本号的互操作程序集?

c# - 如何使用 2 个或更多模型?

c# - 拥有信用卡模型安全吗?

c# - 如何在带有 StringTemplate 的循环中使用 IF 语句