?? on a string

  • Thread starter Thread starter Peter
  • Start date Start date
P

Peter

Hi

I have this code (query is a string):

if ((query ?? "") == "")
return false;

Is that the same as

if (query == null)
return false;

What is the advantage of using ??


Thanks,
Peter
 
This is not the same.

if((query ?? "" ) == "") return false; is equal to

if(querry == null) query = "";
if(query == "") return false;

but you can write it better (in case the return s not inside a try
finally block where query is used in some way)

if(String.IsNullOrEmpty(query)) return false;

the ?? operator means: If the left side is null assign the right side
 
I have this code (query is a string):

if ((query ?? "") == "")
return false;

Is that the same as

if (query == null)
return false;

What is the advantage of using ??

The null-coalescing operator (??) is useful when working with nullable
types---such as a nullable int (int?).

For example, you cannot do the following:

int? x = null;
int y = (int)x;

This code will compile, but at runtime, you'll get a
System.InvalidOperationException because you can't cast a null nullable
type to its equivalent non-nullable type.

To get around this, you'd want to use a default value, á la the null
coalescing operator:

int? x = null;
int y = x ?? 0;

This way, if x is null, you can return some reasonably sane default
when you are constrained to using something that isn't nullable.

In your code sample above:

if((query ?? "") == "")
return(false);

isn't the same as:

if(query == null)
return(false);

Because the comparison is different. It's actually identical to
code in TestValue() below:

public static bool TestValue(string val) {
if(val == null) return(false);
if(val == "") return(false);

// Assumed
return(true);
}

string x = null;
string y = "";
string z = "foo";
bool result;

result = TestValue(x);
result = TestValue(y);

The reason of course being that the string you're checking could be ""
already, and so you can't assume that query (in your code) was null if
it is "" after using ?? on it.

--- Mike
 
This is not the same.

if((query ?? "" ) == "") return false; is equal to

if(querry == null) query = "";
if(query == "") return false;

I don't believe that's correct. The ?? operator does not assign the
right-hand side to the left-hand side if the left-hand side is null.
 
I don't believe that's correct. The ?? operator does not assign the
right-hand side to the left-hand side if the left-hand side is null.

Correct. The null coalescing operator is part of an expression, the
result of which can optionally be assigned into a variable for storage.

--- Mike
 
[...]
Because the comparison is different. It's actually identical to
code in TestValue() below:

public static bool TestValue(string val) {
if(val == null) return(false);
if(val == "") return(false);

// Assumed
return(true);
}

Minor nit: the above code is not "identical" to the literal behavior
of the ?? operator. It produces the same results, but the literally
correct description of "identical" belongs to this code:

if ((val == null ? "" : val) == "")
return false;

That is, the ?? operator doesn't affect the conditional expression,
it affects the resolution of the "val" part of the expression.

Absolutely correct. I was only trying to avoid ?: because it is also
often hard-to-understand for some people.

--- Mike
 
True. Still, one could provide a closer alternative, even without:

string temp;

if (val == null)
temp = "";
else
temp = val;

if (temp == "")
return false;

It seems to me that your example carries with it the implication that
the ?? operator affects how the whole truth expression in the if()
statement is evaluated. Obviously, it'd be bad if someone got that
impression from reading an example like that.

Hrm. I thought representing it as a function would have done away with
that---which is why I didn't put it inline.

Thanks, though, I'll think about that for next time.

--- MIke
 
Back
Top