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. The Lounge
  3. Unions in C#

Unions in C#

Scheduled Pinned Locked Moved The Lounge
csharpcom
30 Posts 16 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.
  • E Offline
    E Offline
    Eytukan
    wrote on last edited by
    #1

    oops just seeing it's not there natively. (Not sure why) but done through interop :)

    using System.Runtime.InteropServices;
    [StructLayout(LayoutKind.Explicit)]
    struct ByteArray {
    [FieldOffset(0)]
    public byte Byte1;
    [FieldOffset(1)]

    Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

    C Mike HankeyM K M Sander RosselS 10 Replies Last reply
    0
    • E Eytukan

      oops just seeing it's not there natively. (Not sure why) but done through interop :)

      using System.Runtime.InteropServices;
      [StructLayout(LayoutKind.Explicit)]
      struct ByteArray {
      [FieldOffset(0)]
      public byte Byte1;
      [FieldOffset(1)]

      Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

      C Offline
      C Offline
      CodeWraith
      wrote on last edited by
      #2

      I thought Nobody Screws With The Union[^].

      I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

      OriginalGriffO E R 3 Replies Last reply
      0
      • C CodeWraith

        I thought Nobody Screws With The Union[^].

        I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

        OriginalGriffO Offline
        OriginalGriffO Offline
        OriginalGriff
        wrote on last edited by
        #3

        Ah, but ... Part of the union[^]

        Sent from my Amstrad PC 1640 Never throw anything away, Griff Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!

        "I have no idea what I did, but I'm taking full credit for it." - ThisOldTony
        "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt

        1 Reply Last reply
        0
        • C CodeWraith

          I thought Nobody Screws With The Union[^].

          I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

          E Offline
          E Offline
          Eytukan
          wrote on last edited by
          #4

          :-D :laugh:

          Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

          1 Reply Last reply
          0
          • E Eytukan

            oops just seeing it's not there natively. (Not sure why) but done through interop :)

            using System.Runtime.InteropServices;
            [StructLayout(LayoutKind.Explicit)]
            struct ByteArray {
            [FieldOffset(0)]
            public byte Byte1;
            [FieldOffset(1)]

            Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

            Mike HankeyM Offline
            Mike HankeyM Offline
            Mike Hankey
            wrote on last edited by
            #5

            When I first woke up this morning and during the pre-caffeination period when my eyes had not quite focused yet I read Unicorns in C#. Kinda the same thing eh?

            Everyone has a photographic memory; some just don't have film. Steven Wright

            P E 2 Replies Last reply
            0
            • Mike HankeyM Mike Hankey

              When I first woke up this morning and during the pre-caffeination period when my eyes had not quite focused yet I read Unicorns in C#. Kinda the same thing eh?

              Everyone has a photographic memory; some just don't have film. Steven Wright

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

              I very much prefer unicorns over unions.

              Mike HankeyM 1 Reply Last reply
              0
              • P PIEBALDconsult

                I very much prefer unicorns over unions.

                Mike HankeyM Offline
                Mike HankeyM Offline
                Mike Hankey
                wrote on last edited by
                #7

                Me too! We don't have much of either of them here in Florida though!

                Everyone has a photographic memory; some just don't have film. Steven Wright

                1 Reply Last reply
                0
                • E Eytukan

                  oops just seeing it's not there natively. (Not sure why) but done through interop :)

                  using System.Runtime.InteropServices;
                  [StructLayout(LayoutKind.Explicit)]
                  struct ByteArray {
                  [FieldOffset(0)]
                  public byte Byte1;
                  [FieldOffset(1)]

                  Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                  K Offline
                  K Offline
                  kalberts
                  wrote on last edited by
                  #8

                  Does anyone remember Fortran COMMON blocks? Unions are sort of like that.

                  E C 2 Replies Last reply
                  0
                  • Mike HankeyM Mike Hankey

                    When I first woke up this morning and during the pre-caffeination period when my eyes had not quite focused yet I read Unicorns in C#. Kinda the same thing eh?

                    Everyone has a photographic memory; some just don't have film. Steven Wright

                    E Offline
                    E Offline
                    Eytukan
                    wrote on last edited by
                    #9

                    :laugh: :thumbsup:

                    Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                    1 Reply Last reply
                    0
                    • K kalberts

                      Does anyone remember Fortran COMMON blocks? Unions are sort of like that.

                      E Offline
                      E Offline
                      Eytukan
                      wrote on last edited by
                      #10

                      I was old enough to touch PASCAL & COBOL. but FORTRAN -No, I missed it by a whisker. My previous batches did have this in their syllabus, in schools. :-O

                      Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                      1 Reply Last reply
                      0
                      • E Eytukan

                        oops just seeing it's not there natively. (Not sure why) but done through interop :)

                        using System.Runtime.InteropServices;
                        [StructLayout(LayoutKind.Explicit)]
                        struct ByteArray {
                        [FieldOffset(0)]
                        public byte Byte1;
                        [FieldOffset(1)]

                        Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                        M Offline
                        M Offline
                        Maximilien
                        wrote on last edited by
                        #11

                        Damn socialists unions, they've taken over C# !!!

                        I'd rather be phishing!

                        1 Reply Last reply
                        0
                        • E Eytukan

                          oops just seeing it's not there natively. (Not sure why) but done through interop :)

                          using System.Runtime.InteropServices;
                          [StructLayout(LayoutKind.Explicit)]
                          struct ByteArray {
                          [FieldOffset(0)]
                          public byte Byte1;
                          [FieldOffset(1)]

                          Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                          K Offline
                          K Offline
                          kalberts
                          wrote on last edited by
                          #12

                          Vunic wrote:

                          oops just seeing it's not there natively. (Not sure why)

                          Union seems to me more or less meaningless in managed code. At least it it contrary to the philosophy of managed code: You don't have a "memory address" but a handle. The runtime system will interpret that handle, translating it to "some" memory address that you shouldn't care about at all. The runtime system has the right to to that handle-to-address mapping in whatever way it pleases: It can do a JIT compilation (for code handles) and put the result into the assembly cache, it can load the data object from some backing store (in principle even magnetic tape!) etc. A union says: Let these two logical objects (usually fields in a class/struct), the two handles, be translated to the same physical address. That would strongly restrict the freedom of the mapping operation of the runtime system. What if the alternative interpretations have different sizes? What if one alternative contains a partial object, addressed through a handle, while another is a flat array? What if one member is an accessor function? Or a delegate? What about a list object unionized with a string? If the semantics of overlaying wasn't unambiguously defined, it would be useless, so you would have to define all the details of cases like these mentiones, and the runtime system would have to imlement exactly that (rather hairy) semantics. I am really happy that we have kicked unions out the back door. If you really need that functionality, then you declare a byte array, to hold the "memory block", and define accessors that select parts of that byte array, and by shifting and masking and explicit casting provides other interpretations of the "memory block". It is not done behind the curtain, where the reader would have to know how the compiler and runtime system works on the bit level. Rather, you code it all yourself, explicitly and with a big warnig sign: Danger! Arbitrary, unchecked casts made here! Then you define the overlaying in every detail, completely independent of how the compiler and runtime system treats your data. Yes, it will cost you a lot of work. It should. To keep you away from doing it.

                          E 1 Reply Last reply
                          0
                          • K kalberts

                            Vunic wrote:

                            oops just seeing it's not there natively. (Not sure why)

                            Union seems to me more or less meaningless in managed code. At least it it contrary to the philosophy of managed code: You don't have a "memory address" but a handle. The runtime system will interpret that handle, translating it to "some" memory address that you shouldn't care about at all. The runtime system has the right to to that handle-to-address mapping in whatever way it pleases: It can do a JIT compilation (for code handles) and put the result into the assembly cache, it can load the data object from some backing store (in principle even magnetic tape!) etc. A union says: Let these two logical objects (usually fields in a class/struct), the two handles, be translated to the same physical address. That would strongly restrict the freedom of the mapping operation of the runtime system. What if the alternative interpretations have different sizes? What if one alternative contains a partial object, addressed through a handle, while another is a flat array? What if one member is an accessor function? Or a delegate? What about a list object unionized with a string? If the semantics of overlaying wasn't unambiguously defined, it would be useless, so you would have to define all the details of cases like these mentiones, and the runtime system would have to imlement exactly that (rather hairy) semantics. I am really happy that we have kicked unions out the back door. If you really need that functionality, then you declare a byte array, to hold the "memory block", and define accessors that select parts of that byte array, and by shifting and masking and explicit casting provides other interpretations of the "memory block". It is not done behind the curtain, where the reader would have to know how the compiler and runtime system works on the bit level. Rather, you code it all yourself, explicitly and with a big warnig sign: Danger! Arbitrary, unchecked casts made here! Then you define the overlaying in every detail, completely independent of how the compiler and runtime system treats your data. Yes, it will cost you a lot of work. It should. To keep you away from doing it.

                            E Offline
                            E Offline
                            Eytukan
                            wrote on last edited by
                            #13

                            *That useful reply* in lounge threads. :) :thumbsup: thanks

                            Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                            J 1 Reply Last reply
                            0
                            • E Eytukan

                              *That useful reply* in lounge threads. :) :thumbsup: thanks

                              Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                              J Offline
                              J Offline
                              jeron1
                              wrote on last edited by
                              #14

                              Vunic wrote:

                              *That useful reply* in lounge threads

                              :confused: Such a thing exists? :laugh:

                              "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                              E 1 Reply Last reply
                              0
                              • J jeron1

                                Vunic wrote:

                                *That useful reply* in lounge threads

                                :confused: Such a thing exists? :laugh:

                                "the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle

                                E Offline
                                E Offline
                                Eytukan
                                wrote on last edited by
                                #15

                                Lol :) Very rare, but sure exists.

                                Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                                1 Reply Last reply
                                0
                                • E Eytukan

                                  oops just seeing it's not there natively. (Not sure why) but done through interop :)

                                  using System.Runtime.InteropServices;
                                  [StructLayout(LayoutKind.Explicit)]
                                  struct ByteArray {
                                  [FieldOffset(0)]
                                  public byte Byte1;
                                  [FieldOffset(1)]

                                  Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                                  Sander RosselS Offline
                                  Sander RosselS Offline
                                  Sander Rossel
                                  wrote on last edited by
                                  #16

                                  var unioned = new[] { "Hi" }.Union(new[] { "I'm unioned" });

                                  From the top of my head, but it's a union and it should compile :D

                                  Best, Sander sanderrossel.com Continuous Integration, Delivery, and Deployment arrgh.js - Bringing LINQ to JavaScript Object-Oriented Programming in C# Succinctly

                                  E 1 Reply Last reply
                                  0
                                  • C CodeWraith

                                    I thought Nobody Screws With The Union[^].

                                    I have lived with several Zen masters - all of them were cats. His last invention was an evil Lasagna. It didn't kill anyone, and it actually tasted pretty good.

                                    R Offline
                                    R Offline
                                    RickZeeland
                                    wrote on last edited by
                                    #17

                                    We have Union in the Netherlands :-\ Union is the city bike brand[^]

                                    1 Reply Last reply
                                    0
                                    • E Eytukan

                                      oops just seeing it's not there natively. (Not sure why) but done through interop :)

                                      using System.Runtime.InteropServices;
                                      [StructLayout(LayoutKind.Explicit)]
                                      struct ByteArray {
                                      [FieldOffset(0)]
                                      public byte Byte1;
                                      [FieldOffset(1)]

                                      Starting to think people post kid pics in their profiles because that was the last time they were cute - Jeremy Falcon.

                                      CPalliniC Offline
                                      CPalliniC Offline
                                      CPallini
                                      wrote on last edited by
                                      #18

                                      Even C++ is flying away from those dirty boulevards[^].

                                      In testa che avete, signor di Ceprano?

                                      E 1 Reply Last reply
                                      0
                                      • K kalberts

                                        Does anyone remember Fortran COMMON blocks? Unions are sort of like that.

                                        C Offline
                                        C Offline
                                        Chris Maunder
                                        wrote on last edited by
                                        #19

                                        My first real programming job was all FORTRAN. 'Cause Real Scientists Program In FORTRAN. It was especially fun when everyone insisted that variables be named a,aa,aaa,b,bb,bbb,b1,bb1 etc. For a bunch of smart people they were truly awful at organising (let along thinking through) code.

                                        cheers Chris Maunder

                                        L K 2 Replies Last reply
                                        0
                                        • C Chris Maunder

                                          My first real programming job was all FORTRAN. 'Cause Real Scientists Program In FORTRAN. It was especially fun when everyone insisted that variables be named a,aa,aaa,b,bb,bbb,b1,bb1 etc. For a bunch of smart people they were truly awful at organising (let along thinking through) code.

                                          cheers Chris Maunder

                                          L Offline
                                          L Offline
                                          Lost User
                                          wrote on last edited by
                                          #20

                                          Chris Maunder wrote:

                                          Cause Real Scientists Program In FORTRAN.

                                          And you were using it why?

                                          Michael Martin Australia "I controlled my laughter and simple said "No,I am very busy,so I can't write any code for you". The moment they heard this all the smiling face turned into a sad looking face and one of them farted. So I had to leave the place as soon as possible." - Mr.Prakash One Fine Saturday. 24/04/2004

                                          C 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