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;


public fixed ulong ID[4];

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

>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.

Leave a Reply