Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Question about fixed data and the memory marshal (C#)

I have a question about the two following pieces of code. I want to know if the would work the same way.

[MarshalAs(UnmanagedType.ByValArray, SizeConst = 4)]
public ulong[] ID;

and

public fixed ulong ID[4];

Haven’t tried any solutions, unsure of what to expect.
Thanks all in advance.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Yes, the two pieces of code produce equivalent results in C#. They both declare an array of ulong values with a fixed size of 4.

The first code uses the MarshalAs attribute to specify that the array should be marshaled as an unmanaged, fixed-size ByValArray.

The second code uses the fixed keyword to declare a fixed-size array of ulong values, with a size of 4.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading