开发者

Java ?: operator in vb.net

开发者 https://www.devze.com 2022-12-17 11:19 出处:网络
Is there a ?: operator equivalent in .net? eg in java I can do: retParts[0] = (emailParts.length > 0) ? emailParts[0] : \"\";

Is there a ?: operator equivalent in .net? eg in java I can do:

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

rather than

if (emailParts.length > 0) {
    retParts[0] = emailParts[0];
} els开发者_StackOverflow社区e {
    retParts[0] = "";
}

I'd like to be able to do similar in VB.NET.


Use the If operator:

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

This operator was introduced in VB.NET 9 (released with .net Framework 3.5). In earlier versions, you will have to resort to the IIf function (no type inference, no short-circuiting):

' always returns Object, always evaluates both ifTrue and ifFalse
... = IIf(condition, ifTrue, ifFalse)    
0

精彩评论

暂无评论...
验证码 换一张
取 消