input == null ? "" : input

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
 
L

Laurent Bugnion

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
 
E

Eliyahu Goldin

? : is a conditional statement. It is a standard statement in all C-based
languages.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top