Export (0) Print
Expand All
1 out of 1 rated this helpful - Rate this topic

Marshal.SizeOf Method (Object)

Returns the unmanaged size of an object in bytes.

Namespace: System.Runtime.InteropServices
Assembly: mscorlib (in mscorlib.dll)

[ComVisibleAttribute(true)] 
public:
static int SizeOf (
	Object^ structure
)
/** @attribute ComVisibleAttribute(true) */ 
public static int SizeOf (
	Object structure
)
ComVisibleAttribute(true) 
public static function SizeOf (
	structure : Object
) : int

Parameters

structure

The object whose size is to be returned.

Return Value

The size of the structure parameter in unmanaged code.
Exception typeCondition

ArgumentNullException

The structure parameter is a null reference (Nothing in Visual Basic).

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 CharSet value applied to that class.

You can use the SizeOf method to determine how much unmanaged memory to allocate using the AllocHGlobal and AllocCoTaskMem methods.

NoteNote

This method uses SecurityAction.LinkDemand to prevent it from being called from untrusted code; only the immediate caller is required to have SecurityPermissionAttribute.UnmanagedCode permission. If your code can be called from partially trusted code, do not pass user input to Marshal class methods without validation. For important limitations on using the LinkDemand member, see Demand vs. LinkDemand.

The following code example creates a managed structure, transfers it to unmanaged memory, and then transfers it back to managed memory. This example uses the SizeOf method to determine how much unmanaged memory to allocate.

No code example is currently available or this language may not be supported.

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.