-2

can someone please Tell me what the ? operator in the line String broker = args.Length > 0 ? args[0] : "localhost:5672"; mean. I came across it when I was reading through this C# Example

MSDN Documentation only seems to have information about the operators ?., ?? and ?[

JOW
  • 244
  • 4
  • 18
  • 5
    https://msdn.microsoft.com/en-us/library/ty67wk28.aspx – Dennis_E Oct 08 '15 at 09:48
  • ?: is a shortcut for if else condition. – Nikita Shrivastava Oct 08 '15 at 09:50
  • Can I just say its kind of nice to see a relatively "easy" question for most people here to actually show research effort and be clear what you're asking... Not sure this is worthy of downvotes – Sayse Oct 08 '15 at 09:52
  • @O.R.Mapper: no, that's something else – M4N Oct 08 '15 at 09:52
  • @O.R.Mapper no. Because the int? are for nullable cahracters. The ... ? .... : ....; itself is just an if then else shortcut and has nothing to do with the nullable characters. – Thomas Oct 08 '15 at 09:52
  • @M4N: Whoops, true. Picked the wrong other question as I was mislead by other questions calling the nullable type marker an "operator". – O. R. Mapper Oct 08 '15 at 09:52
  • 1
    thanks guys. I should have done more homework. '? : ' and '?:' are not the same thing. good ole MSDN gotacha. – JOW Oct 08 '15 at 09:53
  • @JOW no problem there. You found something that you couldn't explain yourself and asked no problem in doing that. Most of the time it is what this site is for ;) – Thomas Oct 08 '15 at 09:53
  • What I don't get though is how thw question became flagged as a duplicate for BOTH other questions oO (the one has nothing to do with it as far as I can see oO – Thomas Oct 08 '15 at 09:55
  • @Thomas - Because a close vote was cast with that duplicate – Sayse Oct 08 '15 at 09:55
  • 1
    @Thomas: The wrong one was my mistake, sorry. I didn't quite like the first question that was pointed out as a duplicate (it didn't seem too duplicate-like to me, as it was asking for *benefits* rather than the meaning), but I was sure the meaning of `?:` had been asked before, so I searched. I found [*What is the meaning of the ? operator?*](http://stackoverflow.com/questions/14561228/what-is-the-meaning-of-the-operator), didn't look closely enough to recognize that the question is not actually talking about an *operator*, but the question mark as a type suffix, and - as that other ... – O. R. Mapper Oct 08 '15 at 09:57
  • ... question had already been marked as a duplicate - automatically picked the alleged duplicate that the other question was linked to [*What is the purpose of a question mark after a type (for example: int? myVariable)?*](http://stackoverflow.com/questions/2690866/what-is-the-purpose-of-a-question-mark-after-a-type-for-example-int-myvariabl). Quickly, before this question gets closed as a duplicate linking to an IMHO not so good duplicate, which is why I again didn't look closely enough :-/ Unfortunately, now I cannot undo this and replace my vote with a duplicate vote for another question. – O. R. Mapper Oct 08 '15 at 09:58
  • no problem OR. Tnx btw didnt know that it then automatically takes all possible duplicates as duplicates – Thomas Oct 08 '15 at 10:02
  • @Thomas: *It* doesn't, *I* usually do. When I cast a close-as-duplicate vote, and the question I find is marked as a duplicate itself, I usually follow the chain of links until I find the question that the community somehow agreed on to be the "original" that remains open (lest future visitors of the duplicate question have to follow an infinite chain of duplicate links before reaching the unclosed question). – O. R. Mapper Oct 08 '15 at 10:04

4 Answers4

5

This is ternary operator

String broker = args.Length > 0 ? args[0] : "localhost:5672"; 

Above statement is same as

if(args.Length > 0)
     broker = args[0];
else
     broker = "localhost:5672"; 
Harshit
  • 5,147
  • 9
  • 46
  • 93
  • Although they are logically equivalent, the emitted IL is slightly different, and it appears the ternary operator allows for some compiler optimizations at the IL level. Of course those are negligible and maintainability of the code should come first. – Ananke Oct 08 '15 at 10:29
2

That's not a "?" operator, it's "? :" - so called ternary operator. Here is a small explanation from MSDN

The ?: operator can be used as a shortcut for an if...else statement. It is typically used as part of a larger expression where an if...else statement would be awkward

Serhiy Chupryk
  • 438
  • 2
  • 5
  • 15
2

The ? operator itself is nothing more than a shortcut for if. Lets take your example here:

String broker = args.Length > 0 ? args[0] : "localhost:5672"

The ? means if. Thus all left is the if part all right of ? is the then part. And the : separates the else part from the then part.

So to put it all together you could also write:

String broker;
if (args.Length > 0)
{
    broker = args[0];
}
else
{
    broker = "localhost:5672";
}

If you use it or not is mostly a matter of taste and how the code is better readable (if you need to do masses of short ifs just one after the next the ? can be better readable than the fully written method for example).

Thomas
  • 2,886
  • 3
  • 34
  • 78
1

This is ternary operator it is equal to

String broker;

if (args.Length > 0) 
{
   broker = args[0];
}
else
{
   broker = "localhost:5672"
}

but a lot shorter to write.

`test ? expression1 : expression2` 

returns expression1 if test is true or expression2 if test is false.

Robert
  • 19,800
  • 5
  • 55
  • 85