Why does this conversion doesn't work?

后端 未结 1 1273
青春惊慌失措
青春惊慌失措 2021-01-22 23:54

following code behaves strange (at least for me):

int testValue = 1234;

this.ConversionTest( testValue );

private void ConversionTest( object value )
{
    lon         


        
相关标签:
1条回答
  • 2021-01-23 00:34

    The value parameter of your ConversionTest method is typed as object; this means that any value types -- for example, int -- passed to the method will be boxed.

    Boxed values can only be unboxed to exactly the same type:

    • When you do (long)(int)value you're first unboxing value to an int (its original type) and then converting that int to a long.
    • When you do (long)value you're attempting to unbox the boxed int to a long, which is illegal.
    0 讨论(0)
提交回复
热议问题