input == null ? "" : input

  • Thread starter Thread starter Jeff
  • Start date Start date
J

Jeff

Hey

I've bought the book "ASP.NET 2.0 website programming, Problem, Design,
Solution" and some of its code examples is this code:

protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}

I have problem understanding this line: (input == null? "":input);
input shall only be set to "" if the value of input is NULL, but I don't see
any if-statement in this code. ?

Best Regards!

Jeff
 
Hi,
Hey

I've bought the book "ASP.NET 2.0 website programming, Problem, Design,
Solution" and some of its code examples is this code:

protected static string ConvertNullToEmptyString(string input)
{
return (input == null ? "" : input);
}

I have problem understanding this line: (input == null? "":input);
input shall only be set to "" if the value of input is NULL, but I don't see
any if-statement in this code. ?

Best Regards!

Jeff

The '?' conditional statement evaluates the first term (after the '?'
and before the ':') if the condition is true, and evaluates the second
term (after the ':') if the condition is false.

In that case, the code ensures that the "null" value is never returned,
but only an empty string. Translated to an "if" expression, it would be:

if ( input == null )
{
return "";
}
else
{
return input;
}

HTH,
Laurent
 
? : is a conditional statement. It is a standard statement in all C-based
languages.
 
Back
Top