Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does the ?? operator do? [duplicate]

Tags:

c#

Possible Duplicate:
What is the “??” operator for?

I have recently come across the ?? operator in C#. What does this operator do and when would someone use it?

Example:

string name = nameVariable ?? string.Empty;
like image 885
brainimus Avatar asked Aug 04 '10 18:08

brainimus


1 Answers

The ?? operator basically means "or if it is null, blah". It is equivalent to:

string name = (nameVariable == null) ? string.Empty : nameVariable;

Which, if you're not familiar with the syntax, is basically:

string name;
if (nameVariable == null)
    name = string.Empty;
else
    name = nameVariable;
like image 109
Lucas Jones Avatar answered Oct 04 '22 19:10

Lucas Jones