Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. very subtle bug. can you figure out whats wrong with this? :)

very subtle bug. can you figure out whats wrong with this? :)

Scheduled Pinned Locked Moved Clever Code
questioncsharpvisual-studiohelp
9 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    Allen Anderson
    wrote on last edited by
    #1

    object x = 2; byte? i = ((byte?)x) ?? 1; First, without throwing this into vs.net. See if you can figure out what is wrong with this expression. Second, it SHOULD exhibit the same behavior if x = null; But it doesn't. Compiler bug maybe?

    P R D J 4 Replies Last reply
    0
    • A Allen Anderson

      object x = 2; byte? i = ((byte?)x) ?? 1; First, without throwing this into vs.net. See if you can figure out what is wrong with this expression. Second, it SHOULD exhibit the same behavior if x = null; But it doesn't. Compiler bug maybe?

      P Offline
      P Offline
      PIEBALDconsult
      wrote on last edited by
      #2

      Makes sense to me. What problem do you have?

      1 Reply Last reply
      0
      • A Allen Anderson

        object x = 2; byte? i = ((byte?)x) ?? 1; First, without throwing this into vs.net. See if you can figure out what is wrong with this expression. Second, it SHOULD exhibit the same behavior if x = null; But it doesn't. Compiler bug maybe?

        R Offline
        R Offline
        Rob Smiley
        wrote on last edited by
        #3

        object x = 2; implies x as an int32, but then why doesn't that work when int x = 2; does? also, object x = (byte)2; works but object x = (int)2; doesn't!

        "An eye for an eye only ends up making the whole world blind"

        A 1 Reply Last reply
        0
        • R Rob Smiley

          object x = 2; implies x as an int32, but then why doesn't that work when int x = 2; does? also, object x = (byte)2; works but object x = (int)2; doesn't!

          "An eye for an eye only ends up making the whole world blind"

          A Offline
          A Offline
          Allen Anderson
          wrote on last edited by
          #4

          close! the (byte?) overrides the int implication on the 2 though. However, you are right about an implied int as the problem. And this is where it gets a bit strange. When the expression evalutates without the ?? (becuase x=2 is not null) then it throws an exception and complains about the fact that the 1 is an implied int (even though its not used). The part that seems like a compiler bug to me is that when you set x=null and the expression evaluates with the 1 being used, it treats the 1 not as an int but as a byte?. bug?

          P D 2 Replies Last reply
          0
          • A Allen Anderson

            close! the (byte?) overrides the int implication on the 2 though. However, you are right about an implied int as the problem. And this is where it gets a bit strange. When the expression evalutates without the ?? (becuase x=2 is not null) then it throws an exception and complains about the fact that the 1 is an implied int (even though its not used). The part that seems like a compiler bug to me is that when you set x=null and the expression evaluates with the 1 being used, it treats the 1 not as an int but as a byte?. bug?

            P Offline
            P Offline
            PIEBALDconsult
            wrote on last edited by
            #5

            No, it's simply that you need an explicit cast from object to int before you can cast the 2 to byte and thereafter to byte?. You don't need an explicit cast from null to byte?. And doesn't the cast of the 1 from int to byte happen at compile-time?

            1 Reply Last reply
            0
            • A Allen Anderson

              object x = 2; byte? i = ((byte?)x) ?? 1; First, without throwing this into vs.net. See if you can figure out what is wrong with this expression. Second, it SHOULD exhibit the same behavior if x = null; But it doesn't. Compiler bug maybe?

              D Offline
              D Offline
              Daniel Grunwald
              wrote on last edited by
              #6

              This is not a compiler bug. It's just that C# has a confusing syntax for casts. "(X)a" can mean one of three different things: unboxing, a conversion, or a cast. C# uses the same syntax for all of them, though there are important differences, and you are running into one of then. Leaving away the nullables:

              object x = 2;
              byte b = (byte)x; // crashes - "object->byte" is unboxing, but "int->byte" is a conversion
              byte b = (byte)(int)x; // works

              With nullables, it's really the same. Only the value "null" is special: there is no 'boxed null', instead, a simple null reference is used. And the null reference can be 'unboxed' to any nullable type.

              int? a = null;
              object b = a; // results in b==null, the type 'int?' is lost
              DateTime? c = (DateTime?)b; // valid

              O 1 Reply Last reply
              0
              • A Allen Anderson

                close! the (byte?) overrides the int implication on the 2 though. However, you are right about an implied int as the problem. And this is where it gets a bit strange. When the expression evalutates without the ?? (becuase x=2 is not null) then it throws an exception and complains about the fact that the 1 is an implied int (even though its not used). The part that seems like a compiler bug to me is that when you set x=null and the expression evaluates with the 1 being used, it treats the 1 not as an int but as a byte?. bug?

                D Offline
                D Offline
                Daniel Grunwald
                wrote on last edited by
                #7

                Allen Anderson wrote:

                the expression evaluates with the 1 being used, it treats the 1 not as an int but as a byte?. bug?

                No.

                byte b = 1;

                Although '1' is an int, it's perfectly valid to assign it to a byte variable. This is a conversion from byte to int, and for compile-time constants this conversion is implicit. The problem on the left side of the ?? is just that a cast that does unboxing cannot do a conversion at the same time. The right side does not do any unboxing, so there conversions are valid.

                1 Reply Last reply
                0
                • D Daniel Grunwald

                  This is not a compiler bug. It's just that C# has a confusing syntax for casts. "(X)a" can mean one of three different things: unboxing, a conversion, or a cast. C# uses the same syntax for all of them, though there are important differences, and you are running into one of then. Leaving away the nullables:

                  object x = 2;
                  byte b = (byte)x; // crashes - "object->byte" is unboxing, but "int->byte" is a conversion
                  byte b = (byte)(int)x; // works

                  With nullables, it's really the same. Only the value "null" is special: there is no 'boxed null', instead, a simple null reference is used. And the null reference can be 'unboxed' to any nullable type.

                  int? a = null;
                  object b = a; // results in b==null, the type 'int?' is lost
                  DateTime? c = (DateTime?)b; // valid

                  O Offline
                  O Offline
                  OregonGhost
                  wrote on last edited by
                  #8

                  That's probably why the Convert class exists. Call Convert.ToByte(object), and it should work even if the object is a boxed Int32. And it's not that confusing. (Actually, the cast thing isn't confusing either - it's just not obvious that cast syntax not necessarily performs a cast :-D ).

                  1 Reply Last reply
                  0
                  • A Allen Anderson

                    object x = 2; byte? i = ((byte?)x) ?? 1; First, without throwing this into vs.net. See if you can figure out what is wrong with this expression. Second, it SHOULD exhibit the same behavior if x = null; But it doesn't. Compiler bug maybe?

                    J Offline
                    J Offline
                    Judah Gabriel Himango
                    wrote on last edited by
                    #9

                    Without specifying the type, the C# compiler will turn 2 into an Int32. It will box the int into the object x. You them attempt to unbox the int into a Nullable. This won't work and will throw a runtime exception. You can only unbox a value into its original value.

                    Religiously blogging on the intarwebs since the early 21st century: Kineti L'Tziyon Judah Himango

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups