C# ?: Conditional Operator

后端 未结 8 1745
醉梦人生
醉梦人生 2020-12-05 06:26

I have this extract of C# 2.0 source code:

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

result = (decimal)(valueFromDatabase          


        
相关标签:
8条回答
  • 2020-12-05 06:54

    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.
    0 讨论(0)
  • 2020-12-05 06:56

    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".

    0 讨论(0)
  • 2020-12-05 06:57

    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".

    0 讨论(0)
  • 2020-12-05 07:06

    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.

    0 讨论(0)
  • 2020-12-05 07:08

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

    0 讨论(0)
  • 2020-12-05 07:10

    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.

    0 讨论(0)
提交回复
热议问题