System.Runtime.InteropServices.Marshal.SizeOf Method

Returns the unmanaged size of an object in bytes.

Syntax

[System.Runtime.InteropServices.ComVisible(true)]
public static int SizeOf (object structure)

Parameters

structure
The object whose size is to be returned.

Returns

The size of the specified object in unmanaged code.

Remarks

This method accepts an instance of a structure, which can be a reference type or a boxed value type. The layout must be sequential or explicit.

The size returned is the size of the unmanaged object. The unmanaged and managed sizes of an object can differ. For character types, the size is affected by the System.Runtime.InteropServices.CharSet value applied to that class.

You can use the Marshal.SizeOf(object) method to determine how much unmanaged memory to allocate using the erload:System.Runtime.InteropServices.Marshal.AllocHGlobal and Marshal.AllocCoTaskMem(int) methods.

Requirements

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 1.0.5000.0, 2.0.0.0, 4.0.0.0