3
var discount = (i == 1) ? definition.SecondPetDiscount ?? definition.AdditionalPetDiscount :
               (i == 2) ? definition.ThirdPetDiscount ?? definition.AdditionalPetDiscount :
               definition.AdditionalPetDiscount;

Could you any one give me an explanation of this code snippet? I know about the ternary operator. But the above is not like that.

The normal ternary operator looks like this:

PaidDate = ( paidDate == null ? DateTime.Now : paidDate);
6
  • 2
    Have you read the documentation at msdn.microsoft.com/en-us/library/ms173224.aspx ? Commented Apr 1, 2013 at 18:38
  • 4
    This is a combination of multiple "conditional" and "null-coalescing" operators. Just add brackets until it makes sense :) Commented Apr 1, 2013 at 18:40
  • FYI it's properly called the conditional operator not the ternary operator Commented Apr 1, 2013 at 18:41
  • Maybe this will explain it better, your same example DateTime? paidDate = null; var date = paidDate ?? DateTime.Now; You can interpret it as "if paidDate is not null, then return its value, otherwise return DateTime.Now. Notice that the first argument needs to be Nullable, in this case DateTime? Commented Apr 1, 2013 at 18:42
  • 1
    Since the expression has very few parentheses it is essential to note that the null coalescing operator ?? has higher precedence than the conditional operator ? :. Commented Apr 1, 2013 at 18:52

6 Answers 6

6

?: is the ternary operator. ?? is the null-coalescing operator. It is used to return the first expression that is not null.

The snippet you posted is making use of both operators. It is equivalent to the following if/else construct:

if (i == 1)
{
    if (definition.SecondPetDiscount != null)
    {
        discount = definition.SecondPetDiscount;
    }
    else
    {
        discount = definition.AdditionalPetDiscount;
    }
}
else if (i == 2)
{
    if (definition.ThirdPetDiscount != null)
    {
        discount = definition.ThirdPetDiscount;
    }
    else
    {
        discount = definition.AdditionalPetDiscount;
    }
}
else
{
    discount = definition.AdditionalPetDiscount;
}

In that snippet they are chaining together multiple ternary operations. It is also sometimes useful to chain together multiple null-coalescing operations, e.g. var foo = a ?? b ?? c.

Sign up to request clarification or add additional context in comments.

Comments

4

That's the null-coalescing operator in C#. It is a shortcut that returns the left side (if the left side is not null) and otherwise returns the right side

Comments

4

This is a mix.

In this statement, there are two conditional (ternary) operations, as well as the null coalescing operator.

The second "option" of the first ternary operation is an entire ternary operation itself.

Comments

2

I think you're confused with the null coalescing operator, or the ??

definition.ThirdPetDiscount ?? definition.AdditionalPetDiscount

it means that if definition.ThirdPetDiscount is null, than use definition.AdditionalPetDiscount

the ternary operator around that behaves like a regular ternary operator. the null-coalescing operator is just nested inside of it, and you also have 1 ternary operator nested in a second ternary operator

Comments

0
var discount = (i == 1) ? definition.SecondPetDiscount ?? definition.AdditionalPetDiscount :
               (i == 2) ? definition.ThirdPetDiscount ?? definition.AdditionalPetDiscount :
               definition.AdditionalPetDiscount;

Of course, the (bool) ? code : code pattern translates to an if - else block.

So we get

var discount;
if( i==1 )
{
    discount = definition.SecondPetDiscount ?? definition.AdditionalPetDiscount;
}
else if( i==2 )
{
    discount = definition.ThirdPetDiscount ?? definition.AdditionalPetDiscount;
}
else
{
    discount = definition.AdditionalPetDiscount;
}

object ?? object is the null coalescing ternary operator. What this does is it will take the first non-null object in the coalescing and return it. There can be any number of objects in the list.

So the var discount here is set to definition.AdditionalPetDiscount if the 2nd or 3rd pet discount is null, or if i is not 1 or 2.

Comments

0

If i is 1 then discount will be set to definition.SecondPetDiscount unless that is null, in which case it will be set to definition.AdditionalPetDiscount.

If i is 2 then discount will be set to definition.ThirdPetDiscount unless that is null, in which case it will be set to definition.AdditionalPetDiscount.

If i is neither 1 nor 2 then it will be set to definition.AdditionalPetDiscount.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.