15

From the project Roslyn, file src\Compilers\CSharp\Portable\Syntax\CSharpSyntaxTree.cs at line 446 there is:

using (var parser = new InternalSyntax.LanguageParser(lexer, oldTree?.GetRoot(), changes))

What is the ?. there?

Does it check whatever oldTree is null and if it's not then it's running the GetRoot method, and if not then what it returns? This is my first assumption (Which might be wrong), but I can't get forward with it. (Confirm it, and/or answer the new question)

I googled What is ?. C# and nothing related came up, it is as if it ignored my ?.(?)

LyingOnTheSky
  • 2,844
  • 1
  • 14
  • 33
  • 1
    It's the [null-propagating operator](https://roslyn.codeplex.com/discussions/540883), and yes it's kind of tricky to google for an operator only made of punctuation characters. – Lucas Trzesniewski Apr 18 '15 at 19:29

1 Answers1

25

It could be Null-Conditional Operator from C# 6.0:

The null-conditional operator conditionally checks for null before invoking the target method and any additional method within the call chain.

In your case, if oldTree is null,

oldTree?.GetRoot()

will return null instead of trying to call GetRoot() and throwing NullReferenceException.

AlexD
  • 32,156
  • 3
  • 71
  • 65