8

.net 中是否有?:等效的运算符?例如在java中我可以做:

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

而不是

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

我希望能够在 VB.NET 中做类似的事情。

4

1 回答 1

10

使用If 运算符

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

此运算符是在 VB.NET 9(与 .net Framework 3.5 一起发布)中引入的。在早期版本中,您将不得不求助于IIf 函数(没有类型推断,没有短路):

' always returns Object, always evaluates both ifTrue and ifFalse
... = IIf(condition, ifTrue, ifFalse)    
于 2010-01-21T10:26:01.747 回答