Bewerken

Delen via


Marshal.SizeOf Method

Definition

Returns the unmanaged size, in bytes, of a class.

Overloads

SizeOf(Object)
Obsolete.

Returns the unmanaged size of an object in bytes.

SizeOf(Type)
Obsolete.

Returns the size of an unmanaged type in bytes.

SizeOf<T>()

Returns the size of an unmanaged type in bytes.

SizeOf<T>(T)

Returns the unmanaged size of an object of a specified type in bytes.

SizeOf(Object)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Caution

SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514

Returns the unmanaged size of an object in bytes.

public:
 static int SizeOf(System::Object ^ structure);
[System.Obsolete("SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514")]
public static int SizeOf (object structure);
public static int SizeOf (object structure);
[System.Runtime.InteropServices.ComVisible(true)]
public static int SizeOf (object structure);
[<System.Obsolete("SizeOf(Object) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296514")>]
static member SizeOf : obj -> int
static member SizeOf : obj -> int
[<System.Runtime.InteropServices.ComVisible(true)>]
static member SizeOf : obj -> int
Public Shared Function SizeOf (structure As Object) As Integer

Parameters

structure
Object

The object whose size is to be returned.

Returns

The size of the specified object in unmanaged code.

Attributes

Exceptions

The structure parameter is null.

Examples

The following 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.

using System;
using System.Runtime.InteropServices;

public struct Point
{
    public int x;
    public int y;
}

class Example
{

    static void Main()
    {

        // Create a point struct.
        Point p;
        p.x = 1;
        p.y = 1;

        Console.WriteLine("The value of first point is " + p.x + " and " + p.y + ".");

        // Initialize unmanged memory to hold the struct.
        IntPtr pnt = Marshal.AllocHGlobal(Marshal.SizeOf(p));

        try
        {

            // Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, false);

            // Create another point.
            Point anotherP;

            // Set this Point to the value of the
            // Point in unmanaged memory.
            anotherP = (Point)Marshal.PtrToStructure(pnt, typeof(Point));

            Console.WriteLine("The value of new point is " + anotherP.x + " and " + anotherP.y + ".");
        }
        finally
        {
            // Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt);
        }
    }
}
Imports System.Runtime.InteropServices



Public Structure Point
    Public x As Integer
    Public y As Integer
End Structure


Module Example


    Sub Main()

        ' Create a point struct.
        Dim p As Point
        p.x = 1
        p.y = 1

        Console.WriteLine("The value of first point is " + p.x.ToString + " and " + p.y.ToString + ".")

        ' Initialize unmanged memory to hold the struct.
        Dim pnt As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(p))

        Try

            ' Copy the struct to unmanaged memory.
            Marshal.StructureToPtr(p, pnt, False)

            ' Create another point.
            Dim anotherP As Point

            ' Set this Point to the value of the 
            ' Point in unmanaged memory. 
            anotherP = CType(Marshal.PtrToStructure(pnt, GetType(Point)), Point)

            Console.WriteLine("The value of new point is " + anotherP.x.ToString + " and " + anotherP.y.ToString + ".")

        Finally
            ' Free the unmanaged memory.
            Marshal.FreeHGlobal(pnt)
        End Try

    End Sub
End Module

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 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.

See also

Applies to

SizeOf(Type)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Caution

SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515

Returns the size of an unmanaged type in bytes.

public:
 static int SizeOf(Type ^ t);
[System.Obsolete("SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515")]
public static int SizeOf (Type t);
public static int SizeOf (Type t);
[<System.Obsolete("SizeOf(Type) may be unavailable in future releases. Instead, use SizeOf<T>(). For more info, go to http://go.microsoft.com/fwlink/?LinkID=296515")>]
static member SizeOf : Type -> int
static member SizeOf : Type -> int
Public Shared Function SizeOf (t As Type) As Integer

Parameters

t
Type

The type whose size is to be returned.

Returns

The size of the specified type in unmanaged code.

Attributes

Exceptions

The t parameter is a generic type definition.

The t parameter is null.

Examples

The following example demonstrates calling the SizeOf method. This code example is part of a larger example provided for the Marshal class.

// Demonstrate the use of the SizeOf method of the Marshal
// class.
Console::WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal::SizeOf(Point::typeid));
Point point;
Console::WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal::SizeOf(point));
// Demonstrate the use of the SizeOf method of the Marshal class.
Console.WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal.SizeOf(typeof(Point)));
Point p = new Point();
Console.WriteLine("Number of bytes needed by a Point object: {0}",
    Marshal.SizeOf(p));
' Demonstrate the use of the SizeOf method of the Marshal class.
Console.WriteLine("Number of bytes needed by a Point object: {0}", Marshal.SizeOf(GetType(Point)))
Dim p As New Point()
Console.WriteLine("Number of bytes needed by a Point object: {0}", Marshal.SizeOf(p))

Remarks

You can use this method when you do not have a structure. The layout must be sequential or explicit.

The size returned is the size of the unmanaged type. 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.

See also

Applies to

SizeOf<T>()

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Returns the size of an unmanaged type in bytes.

public:
generic <typename T>
 static int SizeOf();
public static int SizeOf<T> ();
static member SizeOf : unit -> int
Public Shared Function SizeOf(Of T) () As Integer

Type Parameters

T

The type whose size is to be returned.

Returns

The size, in bytes, of the type that is specified by the T generic type parameter.

Remarks

You can use this method when you do not have a structure. The layout must be sequential or explicit.

The size returned is the size of the unmanaged type. 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.

Applies to

SizeOf<T>(T)

Source:
Marshal.cs
Source:
Marshal.cs
Source:
Marshal.cs

Returns the unmanaged size of an object of a specified type in bytes.

public:
generic <typename T>
 static int SizeOf(T structure);
public static int SizeOf<T> (T structure);
static member SizeOf : 'T -> int
Public Shared Function SizeOf(Of T) (structure As T) As Integer

Type Parameters

T

The type of the structure parameter.

Parameters

structure
T

The object whose size is to be returned.

Returns

The size, in bytes, of the specified object in unmanaged code.

Exceptions

The structure parameter is null.

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

You can use the SizeOf<T>(T) method to determine how much unmanaged memory to allocate by using the AllocHGlobal and AllocCoTaskMem methods.

Applies to