-1

In c# what does mean < ? : in this string?

return (a < b ? c: "text") + " ";
  • 9
    For the benefit of SO users: what C# programming book do you use? They'll want to know what to avoid. – Hans Passant Oct 25 '11 at 10:54
  • Lol at the million near-identical answers posted already! – Tom Chantler Oct 25 '11 at 10:55
  • 2
    15 answers in three minutes?! New record? – Mark Byers Oct 25 '11 at 10:55
  • I feel like I should attempt an answer... – Doozer Blake Oct 25 '11 at 10:59
  • 2
    @Mark, and about half gave it the wrong name! – George Duckett Oct 25 '11 at 11:02
  • 2
    The downvotes are a little harsh, I remember about 6 months ago another developer was looking through a coding standards document and pointed out a ternary operator to me and asked what it was, I had never seen one either so I would assume they either arent commonly used, or not commonly used in some areas. – Purplegoldfish Oct 25 '11 at 11:07
  • 1
    i agree. it's not the worst question i've ever seen, far from it. – RPM1984 Oct 25 '11 at 11:08
  • 5
    Down votes are ridiculous so are some of the comments, this is a question answer website for all levels of programmers or technically gifted people. If you down vote atleast advise why so user can improve or be a bit more helpful not everyones at the same level and you got to account for that. – Anicho Oct 25 '11 at 11:13
  • 2
    @Anicho: Exactly. `An ignorant person is one who doesn't know what you have just found out.` - Will Rogers. – George Duckett Oct 25 '11 at 11:23

15 Answers15

6

It is a ternary operator.

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 :)

El Ronnoco
  • 11,753
  • 5
  • 38
  • 65
2

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" + " ";
}
Mark Byers
  • 811,555
  • 193
  • 1,581
  • 1,452
2

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

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
1

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

Andras Zoltan
  • 41,961
  • 13
  • 104
  • 160
1

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

Tom Chantler
  • 14,753
  • 4
  • 48
  • 53
1

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

Yahia
  • 69,653
  • 9
  • 115
  • 144
  • The single-space string is being tagged onto both conditions. – Jamiec Oct 25 '11 at 10:54
  • 3
    yes - if you look exactly then you see that I included the space in the second return (no need to "add" two literals!). – Yahia Oct 25 '11 at 10:55
1

It's the conditional operator.

Same as this:

if (a < b)
   return "c" + " ";
else 
   return "text" + " ";
RPM1984
  • 72,246
  • 58
  • 225
  • 350
1

It is basically a short way of writing if else condition

if a is less then b
    then return c + " ";
otherwise 
    return "text" + " ";
Haris Hasan
  • 29,856
  • 10
  • 92
  • 122
1

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

ebb
  • 9,297
  • 18
  • 72
  • 123
1

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 + " ";
Justin
  • 84,773
  • 49
  • 224
  • 367
1

It is called a ternary operatordocs. It is short for

var str = "";
if( a < b)
  str = c;
else
  str = "text"
str += " ";
return str;
Jamiec
  • 133,658
  • 13
  • 134
  • 193
1

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.

Moo-Juice
  • 38,257
  • 10
  • 78
  • 128
1

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.

Michael Sondergaard
  • 1,464
  • 10
  • 25
1
if a is greater than b return c + " " else return "text" + " "
chown
  • 51,908
  • 16
  • 134
  • 170
Jase Whatson
  • 4,179
  • 5
  • 36
  • 45
1

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" + " ";
}
Jan S
  • 1,831
  • 15
  • 21