share
Stack OverflowCan you explain what this code fragment is doing?
[-21] [12] CoffeeAddict
[2009-08-18 19:58:12]
[ c# ternary-operator ]
[ http://stackoverflow.com/questions/1296135] [DELETED]
repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;

I don't get it. What is layout == [expression] doing here in this case as it sets StartIndex

Can somebody help explain what this code is doing?

(5) Good question now that it's cleaned up. - Isaac Waller
possible duplicate of How do I remove duplicate items from an array in Perl? - Nick Craver
the first revision of this question is epic imo - Francisco
@coffee Seriously too much caffeine. Also, try to keep it clean when raging at people. - Will
[+19] [2009-08-18 19:59:47] LorenVS

This code is equivalent to:

if(layout == ProductLayout.Grid4Columns)
    repeater.StartIndex = startIndex - 1;
else
    repeater.StartIndex = 0;

Hope that helps

EDIT:

The conditional operator ?: is the only ternary operator in C# (I believe). The first operand is a boolean value, if this is true, the value of the whole expression is the second term, otherwise, the value of the entire expression is the third term.

value = (condition) ? (valueIfConditionIsTrue) : (valueIfConditionIsFalse)


(1) You mean "ternary", not "tertiary" - Marc Gravell
it's not the conditional that's the problem. It's chaining a bunch of stuff together on one line that is lazy code smell - CoffeeAddict
My bad, thanks for the clarification on the operator name... Not one I have to spit out very often - LorenVS
(6) how is that lazy code exactly? that's what the ternary operator was made for.. - Stan R.
(2) There is no chain. There is one conditional, being used in an assignment. The conditional, like all conditionals, has three expressions. - Adam Bellaire
(3) In all seriousness, as much as there isn't a problem with the code as it is written, the one thing about the ternary operator that makes it hard to approach is that its hard to define exactly where the different terms end. A coder who isn't used to the operator, looking at this line sees 4 terms seperated by 3 operators. It's not a problem with his coding style, but unless you're used to it, it can become more difficult to read - LorenVS
lazy as in chaining. The ternary is not lazy, it's chaining to set other variables and chaining is harder to read. - CoffeeAddict
I've used ternaries before but only ONE variable on left side. Why? because it's much easier to read. - CoffeeAddict
EBGreen. How do you assume I do not know what a ternary is. I did before I posted. The part I did not get was the chaining as it was just reading really weird to me. - CoffeeAddict
Is chaining so bad? If you use Linq you chain allot. - David Basarab
@coffeeaddict-"I've used ternaries before but only ONE variable on left side" ...? ... I only see 1 variable on the left side. Is it the lack of parentheses confusing you? - John MacIntyre
no, I'm saying break that ternary out then into its own variable if you're got more than just one variable on the left side of the ternary. I'm talking about ONE on the left side of the ternary. That's not the case here. - CoffeeAddict
I also cannot see more than one variable on the left side. I see only one variable on the left side: repeater.StartIndex. Then there is a single boolean expression: layout == ProductLayout.Grid4Columns and then finally the two return values. You say you would break it out into its own variable, could you show an example please? - Chris Dunaway
1
[+6] [2009-08-18 20:09:02] patros [ACCEPTED]

I don't like it... I feel it's fairly ambiguous about ordering. It's not clear what was intended, and if that matches how it's actually executed.

Is it

repeater.StartIndex = (layout == ProductLayout.Grid4Columns) ? startIndex - 1 : 0;

or

repeater.StartIndex = layout == (ProductLayout.Grid4Columns ? startIndex - 1 : 0);

Things could get screwy if operator precedence is not what you expect, and you're unlucky enough that the types are compatible.

Add the appropriate bracketing, though, and I have no problem with it.


2
[+2] [2009-08-18 20:10:15] Robert Harvey

Although the syntax seems hackish at first glance, after you get used to it it becomes a great shortcut for simple things like null substitution.

public myNormalizedString = someString ? null : "";

yes, that I'm used to. - CoffeeAddict
3
[+2] [2009-08-18 20:00:37] Adam Fox

The ? is a conditional operator that states if condition then x else y on a single line

int x = ( condition ) ? 0 : 1;

Would result in x = 0 if condition == true or x = 1 if condition == false


4
[+2] [2009-08-18 20:00:46] idursun

It is called ternary operator. More information at the following link.

http://msdn.microsoft.com/en-us/library/ty67wk28%28VS.80%29.aspx


(7) Pure myth. It is called the "conditional" operator. It happens to be ternary (as opposed to unary or binary) - Marc Gravell
(1) It's a semantic issue. Its proper name is the conditional operator. However, you can say the ternary operator when referring to it, because it is (typically) the only ternary operator in the language. If your language has multiple ternary operators, that usage would be confusing and incorrect, of course. - Adam Bellaire
It was not the ternary that threw me off. It was the entire line. - CoffeeAddict
5
[+2] [2009-08-18 20:00:55] Reed Copsey

Just break it into pieces:

repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;

repeater.StartIndex = (layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0);

bool criteria = (layout == ProductLayout.Grid4Columns);
repeater.StartIndex = criteria ? startIndex - 1 : 0;

In other words, if layout is equal to Grid4Columns, it will set start index to "startIndex - 1", otherwise, it will set StartIndex to 0.


6
[+2] [2009-08-18 20:01:40] Jakob

Unless I'm mistaken and this does something else in C# than in PHP, that's a ternary expression, which written in verbose form would be:

if ( layout == ProductLayout.Grid4Columns ) {
    repeater.StartIndex = startIndex - 1;
} else {
    repeater.StartIndex = 0;
}

7
[+2] [2009-08-18 20:03:44] Mark

It is not chained just simple use of ?: Much more readable than

if(layout == ProductLayout.Grid4Columns)
{
     repeater.StartIndex = startIndex - 1 ;
}
else
{
    repeater.StartIndex = 0;
}

8
[+1] [2009-08-18 20:03:50] Havenard

Its an Inline If Else Statement [1].

[1] http://www.google.com/search?q=%22Inline+If+Else+Statement%22

9
[+1] [2009-08-18 20:01:12] Steve Gilham

Equivalent to

   if(layout == ProductLayout.Grid4Columns)
    {
      repeater.StartIndex = startIndex - 1;
    }
    else
    {
      repeater.StartIndex = 0;
    }

10
[+1] [2009-08-18 20:46:23] Robert Harvey

When I write a statement like this, I add parentheses to make the intent of the statement clearer:

repeater.StartIndex = 
  (layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0);

+1 I see no good reason for a -1. Especially without comments. - EBGreen
I don't think it is all that more clear for me personally but that is a purely personal decision so I don't see any good reason for a DV. - EBGreen
11
[+1] [2010-03-31 02:10:10] user305615
repeater.StartIndex = layout == ProductLayout.Grid4Columns ? startIndex - 1 : 0;

layer == [expression] => [expression]
What this means that the above statement can be treated like a black back reducing to the following expression:

repeater.StartIndex = (some logic)?0:n ,where n is some value greater than zero. The purpose of n begin a relative position in the grid.


Note, layout holds a reference to a instance of ProductLayout.member. All the code means is that in the special case (i.e. when setting the repeater's startIndex when the layout type is Grid4Columns) then the start index is not 0.


12