.net中是否有等效的?:运算符?
例如,在Java中,我可以执行以下操作:

retParts[0] = (emailParts.length > 0) ? emailParts[0] : "";

而不是
if (emailParts.length > 0) {
    retParts[0] = emailParts[0];
} else {
    retParts[0] = "";
}

我希望能够在VB.NET中执行类似的操作。

最佳答案

使用If operator:

' data type infered from ifTrue and ifFalse
... = If(condition, ifTrue, ifFalse)

该运算符是在VB.NET 9(随.net Framework 3.5发布)中引入的。在早期版本中,您将不得不诉诸IIf function(无类型推断,无短路):
' always returns Object, always evaluates both ifTrue and ifFalse
... = IIf(condition, ifTrue, ifFalse)

10-07 12:39