3

Can someone please explain me the logic reason why I must cast null to int?

When the left argument type can have their both types ?

Insted of doing

  int? k = (DateTime.Now.Ticks%5 > 3 ? 1 : null);

I must do

  int? k = (DateTime.Now.Ticks%5 > 3 ? 1 : (int?) null);

although int? k = null is perfectly valid.

An opposite example :

I didn't have to do it in:

string k = (DateTime.Now.Ticks%5 > 3 ? "lala" : null);

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
  • 7
    See http://stackoverflow.com/questions/10964065/conditional-operator-doesnt-work-with-two-types-that-inherit-the-same-base-type/10964093#10964093 - not an exact duplicate, but close. – Jon Skeet Feb 07 '13 at 08:04
  • Or see [http://stackoverflow.com/questions/330471/c-sharp-why-cant-a-nullable-int-be-assigned-null-as-a-value](http://stackoverflow.com/questions/330471/c-sharp-why-cant-a-nullable-int-be-assigned-null-as-a-value) – Storm Feb 07 '13 at 08:11
  • @JonSkeet _The type of the conditional expression has to be either the type of the second operand or the type of the third operand_ in the first example of mine it cant find a common type. ok. but in my last sample , what is the common type ? string ? null could be anything – Royi Namir Feb 07 '13 at 08:14
  • @RoyiNamir: The type is `string` - `null` can be converted to `string`, whereas it *can't* be converted to `int`. – Jon Skeet Feb 07 '13 at 08:20

2 Answers2

10
int? k = (DateTime.Now.Ticks%5 > 3 ? 1 : (int?) null);

In this case what we have is 1 is int and null is actually null Now the confusion is the ternary operator is confused as what is the return type int or well null and since its int it won't accept null

So you would need to cast it to a nullable int

Now in the other case you have a string and null is perfectly acceptable for a string

Further explanation can be found at Type inference - Eric

The second and third operands of the ?: operator control the type of the conditional expression. Let X and Y be the types of the second and third operands. Then,

  • If X and Y are the same type, then this is the type of the conditional expression.

  • Otherwise, if an implicit conversion exists from X to Y, but not from Y to X, then Y is the type of the conditional expression.

  • Otherwise, if an implicit conversion exists from Y to X, but not from X to Y, then X is the type of the conditional expression.

  • Otherwise, no expression type can be determined, and a compile-time error occurs.

V4Vendetta
  • 37,194
  • 9
  • 78
  • 82
2

Because the compiler doesn't use the type of the variable in the left hand side to determine the type of the expression on the right hand side. First it determines what type the expression is, then it determines if it's possible to put it in the variable.

There is no type close enough that is common between an int and a null value. You have to either make the int value nullable or the null value "intable" for the compiler to find a common ground for the values.

When you have a string and a null value the compiler can simply use one of the types, because a string is already nullable.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005