size of array of structs in bytes
i have an array of structs and i need to get the size of it in bytes
in C++ i can make it using 开发者_开发百科sizeof()
but i need it C#
thnx
Marshal.SizeOf(typeof(MyStruct)) * array.Length
There is the sizeof
operator. However, it can only be used in unsafe context.
There is also a difference to the method proposed in the other answer, namingly:
For all other types, including structs, the sizeof operator can be used only in unsafe code blocks. Although you can use the Marshal.SizeOf method, the value returned by this method is not always the same as the value returned by sizeof. Marshal.SizeOf returns the size after the type has been marshaled, whereas sizeof returns the size as it has been allocated by the common language runtime, including any padding.
source
Example:
unsafe
{
int size = sizeof(MyStruct)*myArray.Length;
}
Buffer.ByteLength(Array) will do exactly what you want.
精彩评论