Home > Back-end >  Question about fixed data and the memory marshal (C#)
Question about fixed data and the memory marshal (C#)

Time:02-06

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.

CodePudding user response:

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.

  • Related