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?
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 .
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.
It is not faster. There is one difference when you can initialize a constant variable depending on some expression: const int x = (a<b) ?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With