Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7828873
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T10:31:06+00:00 2026-06-02T10:31:06+00:00

I have multiple structs that all starts with a header struct. Like this public

  • 0

I have multiple structs that all starts with a header struct. Like this

public struct BaseProtocol {
    public Header header;
    public Footer footer;
};

The header is

public struct Header {
    public Byte start;
    public Byte group;
    public Byte dest;
    public Byte source;
    public Byte code;
    public Byte status;
};

The problem now is that I need to union them with a Byte[]. I tried it with this

[StructLayout( LayoutKind.Explicit, Size=255 )]
public struct RecBuffer {

    [FieldOffset( 0 )]
    public Header header;

    [FieldOffset( 0 )]
    [MarshalAs( UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 255 )]
    public Byte[] buffer;
};

When I fill the buffer with data I can’t get the data from the header. How can I make c# do the same as I can do with union in c++?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T10:31:07+00:00Added an answer on June 2, 2026 at 10:31 am

    Byte[] is a reference type field, which you cannot overlay with a value type field. You need a fixed size buffer and you need to compile it with /unsafe. Like this:

    [StructLayout(LayoutKind.Explicit, Size = 255)]
    public unsafe struct RecBuffer
    {
    
        [FieldOffset(0)]
        public long header;
    
        [FieldOffset(0)]
        [MarshalAs(UnmanagedType.ByValArray, ArraySubType = UnmanagedType.I1, SizeConst = 255)]
        public fixed Byte buffer[255];
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have multiple column headers that spans of multiple column and I would like
I have a vertex defined like this: struct VERTEX { XMFLOAT3 Pos XMFLOAT2 UV
I have multiple applications that share a config file that sets up NLog partially
I have multiple URLs that returns zip files. Most of the files, I'm able
Basically, I have a large number of C structs to keep track of, that
I have some data that is both read and updated by multiple threads. Both
I have a pointer to a struct. I call a routine that determines whether
Hi all I am having a requirement where I have to assign multiple keys
I have some code below. This code is a basic push/pop stack class that
I'd like to do some stuffs before main function. I have multiple source files.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.