why C# can’t implicitly convert a long var to an object var then to ulong?
long a = 0;
Object c = a;
ulong b = (ulong)c; // throw exception here
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
If you box a value type T, you can only unbox it as itself or as a Nullable ( T? ).
Any other cast is invalid.
That’s because a cast from object can never be interpreted as a conversion, whereas the is a conversion between long and ulong.
So this is legal:
This is also legal:
But this is not:
To do what you want to, you have to cast twice: the first is only unboxing, and the second is the actual conversion:
For further information, see this blog post by Eric Lippert.