简体   繁体   中英

Fixed Size Byte Array

public : array<Byte>^ Foo(array<Byte>^ data)

gets dynamic size managed array

but how can I get fixed size managed byte array?

I wanna force C# user to send me 8 byte array; and get 8 bytes back

style:

public : Byte[8] Foo(Byte[8] data)

EDIT:

can any1 explain why its impossbile in safe context?

If you want to force exactly 8 bytes... consider sending a long or ulong instead. Old-school, but it works. It also has the advantage of not needing an object (a byte[] is an object) - it is a pure value-type (a primitive, in this case)

C# does not allow you to do that. You'll simply have to validate the array's length and maybe throw an exception if the length is not 8.

Also, the type of your function can't be Byte[8] ; you'll have to change that to Byte[] .

You can use a fixed size buffer inside a struct. You'll need it to be in an unsafe block though.

unsafe struct fixedLengthByteArrayWrapper
{
    public fixed byte byteArray[8];
}

On the C++ side you'll need to use inline_array to represent this type.

As Marc correctly says, fixed size buffers are no fun to work with. You'll probably find it more convenient to do runtime length checking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM