Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a null conditional operator in Vbnet?

Tags:

c#

vb.net

cIn C# there is a null conditional operator ?. (sometimes called the Elvis operator) like so:

var name = project?.customer?.name;

which doesn't fail but instead return null if project or customer is null.

Is there an equivalent in VB.NET?

Note that I am not looking for If(b, x, y) but the very ?. replacement.

like image 667
LosManos Avatar asked Aug 02 '17 20:08

LosManos


1 Answers

VB also has the null conditional operator (never heard the term 'Elvis' operator):

Dim name = customer?.name

Notes:

  1. Inferred typing in VB requires 'Option Infer On'

  2. I'm pretty sure that your original C# code sample should have been: var name = customer?.name;

like image 96
Dave Doknjas Avatar answered Nov 01 '22 19:11

Dave Doknjas