tags:

views:

383

answers:

6
+4  A: 

it's an equivalent of

(str == null ? string.Empty : str)
Marek Karbarz
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
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
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
+8  A: 

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>".

hmemcpy
+17  A: 

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
Can you explain `str ?? string.Empty` with a detailed syntax? I mean replacing this line with a detailed syntax. :)
Bibhas
Got it.. Thanx.. :)
Bibhas
A: 

Hi, http://msdn.microsoft.com/en-us/library/ms173224.aspx

IordanTanev
This is just a link. Where's the explanation when this link dies? MSDN isn't forever.
Broam
Nothing lasts forever. Stakcoverflow isn't forever too
IordanTanev
A: 
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
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
+2  A: 

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