In c# what does mean < ? : in this string?
return (a < b ? c: "text") + " ";
In c# what does mean < ? : in this string?
return (a < b ? c: "text") + " ";
condition ? truePart : falsePart
It means if a
is less then b
then return c
else return "text"
. Both values will have " "
appended before returning...
Oh, but please don't use the variable names a
, b
and c
in your own code :)
The ? :
is the conditional operator. If the first argument is true, it evaluates and returns the second argument, otherwise it evaluates and returns the third argument.
Your code is roughly equivalent to this:
if (a < b)
{
return c + " ";
}
else
{
return "text" + " ";
}
Here a is being compared with b and is evaluated as a boolean,
So if a < b is true then c is returned
in case it evaluates to false then text is returned.
In either case you have a single space returned additionally in the end
Read more on Conditional Operators and also Relational Operators
if a
is less than(<) b
then(?) return c
otherwise(:) return "text"
Basically there is no < ? :
operator, you're seeing two separate operators, '<
' and the '?:
' operator, which is ternary and therefore takes three inputs (in this order):
1) An expression that is a boolean (in this case the <
comparison)
2) What to return when 1) is true
3) What to return when 1) is false
that means that if a
is less than b
then return c
, otherwise return "text"
and then add " "
to the end in both cases.
MSDN doc here: http://msdn.microsoft.com/en-us/library/ty67wk28(v=VS.100).aspx
it is a ternary operator which basically is a short form of "if-then-else"... the above would be:
if (a < b)
{
return c + " ";
}
else
{
return "text ";
}
For reference see http://msdn.microsoft.com/en-us/library/ty67wk28%28v=VS.100%29.aspx
It's the conditional operator.
Same as this:
if (a < b)
return "c" + " ";
else
return "text" + " ";
It is basically a short way of writing if else condition
if a is less then b
then return c + " ";
otherwise
return "text" + " ";
It's a if statement, using tenary operator. You can read more about it at http://msdn.microsoft.com/en-us/library/ty67wk28(v=vs.80).aspx
Its a conditional operator:
condition ? first_expression : second_expression;
In your case condition
is a < b
and so this translates to
string retVal;
if (a < b)
{
retVal = c;
}
else
{
retVal = "text";
}
return retVal + " ";
It is called a ternary operatordocs. It is short for
var str = "";
if( a < b)
str = c;
else
str = "text"
str += " ";
return str;
This is a Ternary operation (see http://en.wikipedia.org/wiki/Ternary_operation ). Basically, the expression on the left is evaluated. If it evaluates to true, the result is the expression to the left of the :
. If it is false, it is the one after.
In your example, if a is less than b, the result is c, otherwise text
.
The < operator checks if the left operand is smaller than the right operand.
The ? (Also called the ternary operator) yields, in this case, c if a < b and text otherwise.
if a is greater than b return c + " " else return "text" + " "
This is called a conditional operator, which is a type of ternary operator - as it takes 3 variables.
It is equivalent to
if(a < b)
{
return c + " ";
}
else
{
return "text" + " ";
}