10

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

I saw a line of code which states -

return (str ?? string.Empty).Replace(txtFind.Text, txtReplace.Text);

I want to know the exact meaning of this line(i.e. the ?? part)..

Community
  • 1
  • 1
Bibhas Debnath
  • 14,559
  • 17
  • 68
  • 96

5 Answers5

21

It's the null coalescing operator: it returns the first argument if it's non null, and the second argument otherwise. In your example, str ?? string.Empty is essentially being used to swap null strings for empty strings.

It's particularly useful with nullable types, as it allows a default value to be specified:

int? nullableInt = GetNullableInt();
int normalInt = nullableInt ?? 0;

Edit: str ?? string.Empty can be rewritten in terms of the conditional operator as str != null ? str : string.Empty. Without the conditional operator, you'd have to use a more verbose if statement, e.g.:

if (str == null)
{
    str = string.Empty;
}

return str.Replace(txtFind.Text, txtReplace.Text);
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
9

It's called the null coalescing operator. It allows you conditionally select first non-null value from a chain:

string name = null;
string nickname = GetNickname(); // might return null
string result = name ?? nickname ?? "<default>";

The value in result will be either the value of nickname if it's not null, or "<default>".

Igal Tabachnik
  • 31,174
  • 15
  • 92
  • 157
4

it's an equivalent of

(str == null ? string.Empty : str)
Marek Karbarz
  • 28,956
  • 6
  • 53
  • 73
  • In this particular case it is, but if you substitute an arbitrary expression for `str` it becomes more complicated - because the expression would only be evaluated once. – Jon Skeet Feb 18 '10 at 16:07
  • I've never used the null coalescing operator in any setting other than a simple one like this so I'm not sure what you mean. Would you mind posting an example of what you're referring to? – Marek Karbarz Feb 18 '10 at 16:09
  • 1
    What Jon is getting at is that if you say F() ?? G(), that is not **exactly equivalent** to (F() == null ? G() : F()), because that thing calls F() twice. It's actually more like doing an implicit temp = F() and then calculating temp == null ? G() : temp. And actually that's not right either because things get complicated depending on the exact types of the two sides of the operator. We might be inserting casts in there as well. – Eric Lippert Feb 18 '10 at 17:40
4

The ?? operator says that return me the non null value. So, if you have the following code:

string firstName = null; 

string personName = firstName ?? "John Doe"; 

The above code will return "John Doe" since firstName value is null.

That's it!

azamsharp
  • 19,710
  • 36
  • 144
  • 222
1
str ?? String.Empty

could be written as:

if (str == null) {
    return String.Empty;
} else {
    return str;
}

or as a ternary statement:

str == null ? str : String.Empty;
GateKiller
  • 74,180
  • 73
  • 171
  • 204
  • As Jon Skeet added to an earlier answer, your statement is only equivalent in simple cases--the `str` expression is only evaluated once using the `??` operator. – Broam Feb 18 '10 at 16:11