1

I have such method

        public int foo()
        {
            return (int) MainJsonValues[JsonKeys.DP_PORT.Value];
        }

This method should make cast and return this value

There is how I call this method

myObj.foo()

So, it is very strange because as we can see value contains in Dictionary

You can see my debug on screenshot

enter image description here

So, as far as I see this method can't convert from long to int...

Error

enter image description here

What am I doing wrong?

8
  • is and as operators do not cause conversion, only up/downcasts or unboxing
    – Ben Voigt
    Feb 4, 2020 at 15:44
  • @BenVoigt anyway how to make it work?
    – Sirop4ik
    Feb 4, 2020 at 15:45
  • Not generically, no.
    – Ben Voigt
    Feb 4, 2020 at 15:46
  • @PavelAnikhouski no it is not answer my question or you can post what do you mean exactly?
    – Sirop4ik
    Feb 4, 2020 at 15:47
  • You object inside dictionary has long type, you can't cast it to int on the such way Feb 4, 2020 at 15:50

1 Answer 1

8

A boxed value type may only be unboxed to the unboxed type or the nullable unboxed type. That is, if you have a boxed long, you may unbox it to long or long?, but not any other type. (See the comments for details.)

Therefore you need to do the conversion in two steps:

object x = 1L; // boxed long
long y = (long)x; // unboxed
int z = (int)y;
11
  • "not any other type"... not even ulong?
    – Ben Voigt
    Feb 4, 2020 at 16:06
  • 1
    @BenVoigt: I was wondering if anyone was going to bring that up! ulong is an interesting case. The rule in C# is that an unboxing unboxes to the boxed type. However, the rule of the CLR is that unboxing unboxes to a type that has the same representation. An unboxed ulong has the same bits as a long, we just assign values to those bits differently, and the CLR allows an unboxing from boxed long to unboxed ulong and vice versa. The C# compiler could emit code to prevent this from working, but why make bigger, slower code just to enforce that rule? Feb 4, 2020 at 16:19
  • 1
    @BenVoigt: Similarly the CLR allows a reference conversion from long[] to ulong[] and even though that would be illegal according to the strict rules of C#, you can write a C# program that does so. There are a bunch of situations where the C# design team and the CLR design team did not get quite aligned. Feb 4, 2020 at 16:21
  • 1
    Oh, I understand what you're saying. You're saying that the compiler could deduce "this expression will never be of the given type, therefore I can generate it as though as returned null", but the optimization would produce a different result than runtime. Yeah, there are a bunch of optimizations like that where the compiler team cannot add them because it would break these compat scenarios. Basically once you cast to object or dynamic the compiler stops trying to do optimizations, figuring if you wanted an optimization you would not have inserted the cast! Feb 4, 2020 at 16:32
  • 1
    Eric explains why casting works this way on ericlippert.com/2009/03/03/representation-and-identity .
    – Brian
    Feb 4, 2020 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.