Dave
Dave

Reputation: 7359

vb.net: How is it ? or ?? conditional in c# for vb.net?

In C# we have this conditional assignment:

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

Or

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

Is it possible to do it in vb.net?

I am using to program in c# but in this project I am programming in vb.net, and I don't remember if it is possible to do it.

Upvotes: 1

Views: 169

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460068

It is the If-operator which can be used with one or two parameters. The null-coalescing operator(??) in C# is the If with one parameter and the conditional operator(?) is the one with two parameters.

"Conditional-Operator"

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

"Null-Coalescing-Operator"

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

Note that the If-operator is not the same as the old IIf-function. : Performance difference between IIf() and If

Upvotes: 2

Related Questions