Java?:vb.net中的运算符

the*_*ase 8 java vb.net language-features conditional-operator

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

retParts[0] = (emailParts.length > 0) ? emailParts[0] : "";
Run Code Online (Sandbox Code Playgroud)

而不是

if (emailParts.length > 0) {
    retParts[0] = emailParts[0];
} else {
    retParts[0] = "";
}
Run Code Online (Sandbox Code Playgroud)

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

Hei*_*nzi 10

使用If运算符:

' data type infered from ifTrue and ifFalse
... = If(condition, ifTrue, ifFalse)     
Run Code Online (Sandbox Code Playgroud)

该操作符是在VB.NET 9(随.net Framework 3.5发布)中引入的.在早期版本中,您将不得不求助于IIf函数(无类型推断,无短路):

' always returns Object, always evaluates both ifTrue and ifFalse
... = IIf(condition, ifTrue, ifFalse)    
Run Code Online (Sandbox Code Playgroud)