locked
Implement union contains array fails, how to fix it? RRS feed

  • Question

  • Dear all,

    Implement union contains array fails, how to fix it?

    Thanks and Best regards,

    E-John

        [StructLayout(LayoutKind.Explicit, Size = 6, Pack = 1)]
        public struct UnionArrayStruct
        {
            [FieldOffset(0)]
            public byte[] Bytes;
            [FieldOffset(0)]
            public UInt16 Offset;
    
            public UnionArrayStruct(int dummyValue)
            {
                Bytes = new byte[6];
                Offset = 0x0000;
            }
        }
    
            static void Main(string[] args)
            {
                UnionArrayStruct a = new UnionArrayStruct(0);
                a.Bytes[0] = 0x01;
                Console.ReadLine();
            }
    
    

    Tuesday, June 23, 2020 2:44 PM

Answers

  • Why should this work?

    What do you like to implement?

    You can do something like this:

    namespace ConsoleCS
    {
        using System;
        using System.Runtime.InteropServices;
    
        [StructLayout(LayoutKind.Explicit, Size = 2)]
        public unsafe struct UnionArrayStruct
        {        
            [FieldOffset(0)]
            public fixed byte Bytes[2];
            [FieldOffset(0)]
            public UInt16 Offset;
    
            public UnionArrayStruct(int dummyValue)
            {            
                Offset = 0x0000;
            }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                unsafe
                {
                    UnionArrayStruct a = new UnionArrayStruct(0);
                    a.Bytes[0] = 0x01;
                }
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }    
        }
    }

    • Marked as answer by E-John Wednesday, June 24, 2020 12:56 AM
    Tuesday, June 23, 2020 3:09 PM

All replies

  • Why should this work?

    What do you like to implement?

    You can do something like this:

    namespace ConsoleCS
    {
        using System;
        using System.Runtime.InteropServices;
    
        [StructLayout(LayoutKind.Explicit, Size = 2)]
        public unsafe struct UnionArrayStruct
        {        
            [FieldOffset(0)]
            public fixed byte Bytes[2];
            [FieldOffset(0)]
            public UInt16 Offset;
    
            public UnionArrayStruct(int dummyValue)
            {            
                Offset = 0x0000;
            }
        }
    
        public class Program
        {
            public static void Main(string[] args)
            {
                unsafe
                {
                    UnionArrayStruct a = new UnionArrayStruct(0);
                    a.Bytes[0] = 0x01;
                }
    
                Console.WriteLine("\nDone.");
                Console.ReadLine();
            }    
        }
    }

    • Marked as answer by E-John Wednesday, June 24, 2020 12:56 AM
    Tuesday, June 23, 2020 3:09 PM
  • Dear Stefan,

    Understood, thanks for your helps.

    Best regards,

    E-John

    Wednesday, June 24, 2020 12:56 AM