2014-12-02 40 views
1

在C#中,我们有这样的有条件分配:vb.net:它是怎么回事?要么 ??条件在C#为VB.NET?

var test = 1; 
var something = (test == 1)? "P":"C"; 

或者

var test = null; 
var something = test ?? ""; 

是否有可能做到这一点在vb.net?

我正在使用c#编程,但在这个项目中,我在vb.net编程,我不记得是否有可能做到这一点。

+0

一重复:http://stackoverflow.com/questions/576431/is-there-a-conditional-ternary-operator-in-vb-net – 2014-12-02 12:20:25

+0

另一个重复:http:// stacko verflow.com/questions/6792729/vb-net-null-coalescing-operator – Mephy 2014-12-02 12:22:20

+0

另一个问题是通过使用只有两个参数的“if”版本回答的 – 2014-12-02 12:22:25

回答

2

这是If-operator它可以与一个或两个参数一起使用。 C#中的空合并运算符(??)是带有一个参数的If,条件运算符(?)是带有两个参数的运算符。

“有条件的运营商”

Dim test As Int32 = 1 
Dim something As String = If(test = 1, "P", "C") 

“空 - 结合运营商”

Dim test As String = Nothing 
Dim something As String = If(test, "") ' "" is the replacement value for null ' 

注意,If - 运算符是不一样的老IIf-function.Performance difference between IIf() and If