Akrem
Akrem

Reputation: 4652

differences between the conditional operator and an if-else:

I read this question and I found Tim Schmelter's answer:

By the way, that's one of the differences between the conditional operator and an if-else

you can check the answer for this question, and I can't found the reason ?

if conditional operator work like if-else why if else don't need the cast

question :

int? l = lc.HasValue ? (int)lc.Value : null; 

"Tim Schmelter" answer :

You have to cast null as well:

int? l = lc.HasValue ? (int)lc.Value : (int?)null; 

By the way, that's one of the differences between the conditional operator and an if-else:

if (lc.HasValue)
    l = (int)lc.Value;
else
    l = null;  // works

Upvotes: 2

Views: 3784

Answers (3)

Jeppe Stig Nielsen
Jeppe Stig Nielsen

Reputation: 61942

The literal null alone does not have a type, but it is implicitly convertible to any reference type and to any Nullable<> type. In the expression:

x = null

where x is assigned to null, the compiler can easily infer from the type of the variable (or field or property or parameter or whatever) x what the null literal shall be converted into. For example if x is of type string, the null shall represent the null reference, while if x is of type int?, the null shall represent an instance of Nullable<int> where HasValue is false.

If x is of type int, no implicit conversion exists, and the above expression shall not compile.

(The declaration var x = null; with var is not legal since null has no type in itself.)

On the other hand, in the expression:

someBoolean ? 42 : null /* illegal */

the compiler cannot figure out what type to convert null into. Remember that int is neither reference type, nor Nullable<> type.

If you meant wrapping into a nullable, use:

someBoolean ? (int?)42 : null

or:

someBoolean ? 42 : (int?)null

In both cases the compiler will automatically see that the other operand (on the other side of the colon :) must also be implicitly converted to int?.

If you meant boxing into some base class or interface of int, write that:

someBoolean ? (object)42 : null

or:

someBoolean ? 42 : (object)null

Now, the expressions above could be sub-expressions of a greater containing expression, but the compiler will still need the type of the ?: expression to be clear by itself. For example in:

int? x;
x = someBoolean ? 42 : null; // still illegal!

even if the sub-expression someBoolean ? 42 : null appears inside a larger expression x = someBoolean ? 42 : null where x does have a type, the sub-expression must still acquire its type "intrinsically". The type of x cannot "leak onto" the sub-expression. This "grammar" seems to be a surprise to many new C# developers. Questions like yours are often seen, see e.g. Nullable type issue with ?: Conditional Operator and the threads linked to it.

Upvotes: 5

AdelNick
AdelNick

Reputation: 993

MSDN says for the conditional operator :

*Either the type of first_expression and second_expression must be the same, or an implicit conversion must exist from one type to the other.*

This is the reason. In the if-else constructor the code in else block is independent from the code in if block so the type-casting can be inferred by the compiler in each case.

Upvotes: 0

Sean
Sean

Reputation: 62472

The if statement doesn't yield a value, so the statements in the "then" and "else" parts don't need to be type compatible in any way.

The conditional operator yields a value, and therefore both parts must be type compatible in some way in order for the compiler to determine the type of the expression.

Upvotes: 1

Related Questions