Ioannis
Ioannis

Reputation: 519

Casting with conditional/ternary ("?:") operator

I have this extract of C# source code:

object valueFromDatabase;
decimal result;
valueFromDatabase = DBNull.Value;

result = (decimal)(valueFromDatabase != DBNull.Value ? valueFromDatabase : 0);
result = (valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0);

The first result evaluation throws an InvalidCastException whereas the second one does not. What is the difference between these two?

Upvotes: 42

Views: 18413

Answers (8)

Eric Lippert
Eric Lippert

Reputation: 660128

UPDATE: This question was the subject of my blog on May 27th 2010. Thanks for the great question!

There are a great many very confusing answers here. Let me try to precisely answer your question. Let's simplify this down:

object value = whatever;
bool condition = something;
decimal result = (decimal)(condition ? value : 0);

How does the compiler interpret the last line? The problem faced by the compiler is that the type of the conditional expression must be consistent for both branches; the language rules do not allow you to return object on one branch and int on the other. The choices are object and int. Every int is convertible to object but not every object is convertible to int, so the compiler chooses object. Therefore this is the same as

decimal result = (decimal)(condition ? (object)value : (object)0);

Therefore the zero returned is a boxed int.

You then unbox the int to decimal. It is illegal to unbox a boxed int to decimal. For the reasons why, see my blog article on that subject:

Representation and Identity

Basically, your problem is that you're acting as though the cast to decimal were distributed, like this:

decimal result = condition ? (decimal)value : (decimal)0;

But as we've seen, that is not what

decimal result = (decimal)(condition ? value : 0);

means. That means "make both alternatives into objects and then unbox the resulting object".

Upvotes: 102

Your answer would work if you combined both:

result = (decimal)(valueFromDatabase != DBNull.Value ? (decimal)valueFromDatabase : (decimal)0);

At least, a similar situation casting into a parameter for me.

Upvotes: -1

Dzmitry Huba
Dzmitry Huba

Reputation: 4521

The type of the operator will be object and in case the result must be 0 it will be implicitly boxed. But 0 literal is by default has int type so you box int. But with explicit cast to decimal you try to unbox it which is not permitted (boxed type must much with the one you cast back to). That is why you can get exception.

Here is an excerpt from C# Specification:

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 (§6.1) 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 (§6.1) 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.

Upvotes: 5

Guffa
Guffa

Reputation: 700362

The difference is that the compiler can not determine a data type that is a good match between Object and Int32.

You can explicitly cast the int value to object to get the same data type in the second and third operand so that it compiles, but that of couse means that you are boxing and unboxing the value:

result = (decimal)(valueFromDatabase != DBNull.value ? valueFromDatabase : (object)0);

That will compile, but not run. You have to box a decimal value to unbox as a decimal value:

result = (decimal)(valueFromDatabase != DBNull.value ? valueFromDatabase : (object)0M);

Upvotes: 12

Philippe Leybaert
Philippe Leybaert

Reputation: 171784

Your line should be:

result = valueFromDatabase != DBNull.value ? (decimal)valueFromDatabase : 0m;

0m is the decimal constant for zero

Both parts of a conditional operator should evaluate to the same data type

Upvotes: 4

Ioannis
Ioannis

Reputation: 519

There are two different types for the compiler to decide (at compile time) which one to cast to decimal. This it can't do.

Upvotes: 0

Mark
Mark

Reputation: 2432

Unless I'm mistaken (which is very possible) its actually the 0 that's causing the exception, and this is down to .NET (crazily) assuming the type of a literal so you need to specify 0m rather than just 0.

See MSDN for more info.

Upvotes: 2

Donald Byrd
Donald Byrd

Reputation: 7778

The x : y part need a common type, the database's value is likely some kind of float and 0 is an int. This happens before the cast to decimal. Try ": 0.0" or ": 0D".

Upvotes: 3

Related Questions