C# has the ?? operator, what about VB

A nice VB statement is IF(). It’s equivalent to the C# ?? operator. It works like this:

Dim i as integer? = Nothing
Console.WriteLine(IF(i, –1))
i = 3
Console.WriteLine(IF(i, –1))

The above will output:
-1
3

The IF() statement evaluates the nullable variable. If it is not null, then it will return the value of the variable. Otherwise, it returns the value of the second parameter.

The equivalent in C# would be:

int? i = null;
Console.WriteLine(i ?? -1);
i = 3;
Console.WriteLine(i ?? -1);

The above will also output:
-1
3

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a website or blog at WordPress.com

Up ↑

%d bloggers like this: