1

I have been in a conference and speaker's example has '?.' operator. What is it?

Similar code:

var result = man?.Name;
Sasha Truf
  • 535
  • 5
  • 19
  • 1
    already this question is answered at SO.please make a research before asking a question – Tharif May 19 '15 at 11:23
  • This is indeed not valid, it might be, You typoed it or someone else. Look for this: https://msdn.microsoft.com/en-us/library/1t3y8s4s.aspx - damn, C#6 ? Ok, will remember that. – icbytes May 19 '15 at 11:23
  • @utility I've tried really, but google and stackoverflow find nothing :-( It's very complicated to find something with two symbols '?.' – Sasha Truf May 19 '15 at 11:35

2 Answers2

6

It's c# 6.0 syntax, Null propagation Operator. It means :

var p = man;

if(p != null)
{
   var result = man.Name;
}
else
{
   var result = null;
}

More info here: https://msdn.microsoft.com/en-us/magazine/dn802602.aspx

Milen
  • 8,697
  • 7
  • 43
  • 57
5

It is called Null-propagating operator in C#-6.0 version.

var result = man?.Name;

is equal to

var temp = man;
var result = (temp != null) ? man.Name : null;
Soner Gönül
  • 97,193
  • 102
  • 206
  • 364