.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)
关于 java ? : operator in vb. 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2108393/