Is there a conditional ternary operator in VB.NET?

前端 未结 3 523
北海茫月
北海茫月 2020-11-22 11:31

In Perl (and other languages) a conditional ternary operator can be expressed like this:

my $foo = $bar == $buz ? $cat : $dog;

Is there a s

3条回答
  •  伪装坚强ぢ
    2020-11-22 12:06

    If() is the closest equivalent but beware of implicit conversions going on if you have set "Option Strict off"

    For example, if your not careful you may be tempted to try something like:

    Dim foo As Integer? = If(someTrueExpression, Nothing, 2)
    

    Will give "foo" a value of 0!

    I think the '?' operator equivalent in C# would instead fail compilation

提交回复
热议问题