Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do ternary operator and if statement return different results? [duplicate]

In the following example, I'm returning a DateTimeOffset? using the default value

var a = ConvertToDateTimeOffsetA(null); // 1/1/0001 12:00:00 AM +00:00
var b = ConvertToDateTimeOffsetB(null); // null

private static DateTimeOffset? ConvertToDateTimeOffsetA(DateTime? date)
{
    return date != null
        ? new DateTimeOffset(date.Value, TimeSpan.Zero)
        : default;
}
private static DateTimeOffset? ConvertToDateTimeOffsetB(DateTime? date)
{
    if (date != null) 
        return new DateTimeOffset(date.Value, TimeSpan.Zero);
    return default;
}

Why is there a difference between the returned output in the ternary compared to the if statement?

My guess would be the ternary first coerces the type to DateTimeOffset and then inline converts back to Nullable<DateTimeOffset>, but I'm not quite sure why?

like image 597
KyleMit Avatar asked Apr 08 '21 17:04

KyleMit


People also ask

What is the main difference between the if statement and the conditional ternary operator?

Functional difference: unifying result type The if / else statement doesn't trigger this conversion: the true / 1 branch calls is(int) . You can't use expressions with an overall type of void in a conditional operator either, whereas they're valid in statements under an if / else .

Is ternary operator better than if?

Moreover, as has been pointed out, at the byte code level there's really no difference between the ternary operator and if-then-else. As in the above example, the decision on which to choose is based wholly on readability.

Is ternary operator faster than if C++?

It is not faster. There is one difference when you can initialize a constant variable depending on some expression: const int x = (a<b) ?

Is ternary operator faster than if JavaScript?

There is no difference in speed. Some prefer the if/else for readability. Personally, I use the ternary operator whenever the logic is trivial enough to understand on one line.

What is the difference between IF/ELSE and ternary operator?

Code size wise, the two are essentially equivalent, yet each branch of the if/else statement will execute fewer instructions which should result in faster execution. We can use both the ternary operator and if/else statement to get equivalent results in our code.

What is the ternary operator?

This tutorial will help you learn how to replace an if/else statement with a more concise shorthand syntax called the ternary operator. The conditional operator – also known as the ternary operator – is an alternative form of the if/else statement that helps you to write conditional code blocks in a more concise way.

What are the advantages of ternary operators over plain if statements?

Nicer with pointer data members first, but still fragile. The only potential benefit to ternary operators over plain if statements in my view is their ability to be used for initializations, which is particularly useful for const: Doing this with an if/else block is impossible without using a function cal as well.

What is the conditional ternary operator in JavaScript?

The conditional ternary operator in JavaScript assigns a value to a variable based on some condition and is the only JavaScript operator that takes three operands. The ternary operator is a substitute for an if-else statement. The ternary operator shortens the if-else statement into a single line.


1 Answers

In the ternary version, it is interpreting your default as default(DateTimeOffset), the other output expression type in the conditional. Then it is interpreting the ternary as a whole as a nullable, which will never be null.

In the second case, your return is using default(DateTimeOffset?), from the declared return type.

In this case, you may want to use an explicit type in the default expression, or: just use the second form and add a comment (and ideally a unit test), so nobody "fixes" it in the future.

like image 134
Marc Gravell Avatar answered Oct 19 '22 13:10

Marc Gravell