Total noob here so be gentle. I've looked everywhere and can't seem to find the answer to this. How do I condense the following?
if (expression)
{
return true;
}
else
{
return false;
}
I can't get it to work since it's returning something vs. setting something. I've already seen things like this:
somevar = (expression) ? value1 : value2;
Like I said, please be gentle :)
This question is related to
c#
if-statement
return (expression) ? value1 : value2;
If value1
and value2
are actually true
and false
like in your example, you may as well just
return expression;
All you'd need in your case is:
return expression;
The reason why is that the expression itself evaluates to a boolean value of true
or false
, so it's redundant to have an if
block (or even a ?:
operator).
Since expression is boolean:
return expression;
If expression
returns a boolean, you can just return the result of it.
Example
return (a > b)
Source: Stackoverflow.com