Export (0) Print
Expand All

StructLayoutAttribute.Pack Field

Controls the alignment of data fields of a class or structure in memory.

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

val mutable Pack: int

This field indicates memory boundaries for aligning fields. It affects both LayoutKind.Sequential and LayoutKind.Explicit. By default, the value is 0, indicating the default packing size for the current platform.

The value of Pack must be 0, 1, 2, 4, 8, 16, 32, 64, or 128:

  • A value of 0 indicates that the packing alignment is set to the default for the current platform.

  • A value of 1 indicates that data alignment occurs on byte boundaries. There are no gaps between fields with a packing value of 1.

  • Packing values of 2 and higher will cause each field to be aligned on a byte offset relative to the beginning of the structure. Therefore, data fields will start on offsets that are multiples of the requested packing value.

The following example contains a structure with three one-byte fields:

struct MyStruct
{
byte B0;
byte B1;
byte B2;
}

Byte B0 always starts at offset 0 (byte 0) regardless of the packing value.

A packing value of 0 will produce the following:

  • B0 will begin at offset 0 (byte 0).

  • B1 will begin at offset 1 (byte 1).

  • B2 will begin at offset 2 (byte 2).

Note that the default platform alignment will always pack identical types contiguously.

A packing value of 1 will produce the following:

  • B0 will still begin at offset 0 (byte 0).

  • B1 will still begin at offset 1 (byte 1).

  • B2 will still begin at offset 2 (byte 2).

However, a packing value of 2 will produce the following:

  • B0 will still begin at offset 0 (byte 0).

  • B1 will begin at offset 2 (byte 2).

  • B2 will begin at offset 4 (byte 4).

Similarily, a packing value of n will produce the following:

  • B0 will still begin at offset 0 (byte 0).

  • B1 will begin at offset n (byte n).

  • B2 will begin at offset n*2 (byte 2n).

The Pack field is frequently used when structures are exported during disk and network write operations. The field is also frequently used during platform invoke and interop operations.

Occasionally, the field is used to reduce memory requirements by producing a tighter packing size. However, this usage requires careful consideration of actual hardware constraints, and may actually degrade performance.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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

Show:
© 2014 Microsoft