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.

C#
[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);
C#
public static int SizeOf (object structure);
C#
[System.Runtime.InteropServices.ComVisible(true)]
public static int SizeOf (object structure);

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.

C#
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);
        }
    }
}

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

.NET 9 and other versions
Product Versions (Obsolete)
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 (Core 1.0)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 2.0, 2.1 (1.2, 1.3, 1.4, 1.5, 1.6)
UWP (10.0)

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.

C#
[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);
C#
public static int SizeOf (Type t);

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.

C#
// 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));

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

.NET 9 and other versions
Product Versions (Obsolete)
.NET Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9 (Core 1.0)
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.1, 2.0, 2.1 (1.2, 1.3, 1.4, 1.5, 1.6)
UWP (10.0)

SizeOf<T>()

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

Returns the size of an unmanaged type in bytes.

C#
public static int SizeOf<T> ();

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

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.

C#
public static int SizeOf<T> (T structure);

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

.NET 9 and other versions
Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0