Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ?. mean in C#? [duplicate]

Tags:

operators

c#

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 ?.(?)

like image 405
LyingOnTheSky Avatar asked Apr 18 '15 19:04

LyingOnTheSky


People also ask

What does %= mean in C?

%= Modulus AND assignment operator. It takes modulus using two operands and assigns the result to the left operand. C %= A is equivalent to C = C % A.

What does the || mean in C?

The logical OR operator ( || ) returns the boolean value true if either or both operands is true and returns false otherwise.


1 Answers

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.

like image 117
AlexD Avatar answered Nov 02 '22 18:11

AlexD