2014-12-02 46 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