15

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.

LosManos
  • 7,195
  • 6
  • 56
  • 107
  • I am surprised **I** haven't found any answer when searching and nothing in Stack overflow. – LosManos Aug 02 '17 at 20:35
  • https://stackoverflow.com/questions/31730494/null-propagation-operator-error-bc36716-in-asp-net-4-6-targeted-view – Steve Aug 02 '17 at 20:42
  • 1
    Please type in your C# sample again without typos. It's hard to tell exactly what your question is (I count 2 typos - in addition to the obscure 'Elvis' naming). – Dave Doknjas Aug 02 '17 at 20:47
  • @DaveDoknjas In fairness, "Elvis operator" was a commonly used name when the operator didn't have an official name yet. –  Aug 02 '17 at 20:55
  • 1
    @DaveDoknjas Yes, in other languages, "Elvis operator" refers to `?:`, but C# never had `?:`. When C# gained `?.`, the same name was used unofficially. –  Aug 02 '17 at 20:57
  • @hvd: Thanks for clearing that up - so yes the original question is about the 'Elvis operator' (but should be rewritten to actually be coherent C#). – Dave Doknjas Aug 02 '17 at 21:00
  • Thanks - now I have something (Elvis operator) that I can bring up at parties (maybe not). – Dave Doknjas Aug 02 '17 at 21:04
  • It seems that I was wrong in multiple ways. First the code typo which was a **big** error on my side but was thankfully fixed by @zdeněk-jelínek. Then there was my overuse of the term Elvis operator. See [wikipedia](https://en.wikipedia.org/wiki/Elvis_operator) which states that C# does not really have an Elvis operator. A bit funny though that my typo looked like [the real Elvis operator](https://en.wikipedia.org/wiki/Elvis_operator). Third, and hopefully last, there was my brain that seemed to be unlucky when thinking and didn't recognise the operator was the same in C# and Vbnet. – LosManos Aug 02 '17 at 21:36

1 Answers1

19

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;
    
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28
  • 1. I believe `Option Infer On` is set as default. 2. A typo of mine. Thank you for correcting. The syntax is the same in C# and Vbnet. Schtoopid of me. – LosManos Aug 02 '17 at 21:40
  • @Enigmativity: Yeah - saw that when I looked it up, but had never heard anyone call it that before today. – Dave Doknjas Aug 03 '17 at 00:59
  • @DaveDoknjas The term "Elvis operator" was actually used by C# language design team members (I believe it was Mads Torgersen) in the C#6 spotlight video for the _null-propagating operator_ feature. While the wikipedia page specifically mentions the Elvis operator as being ternary conditional, it also has a link to the safe navigation operator in the See Also part. So it's not that it's entirely wrong. – Zdeněk Jelínek Aug 03 '17 at 09:05