RuntimeInfrastructure mscorlib [00 00 00 00 00 00 00 00 04 00 00 00 00 00 00 00 ] 1.0.5000.0 2.0.0.0 4.0.0.0 All public static members of this type are safe for multithreaded operations. No instance members are guaranteed to be thread safe. System.Attribute System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Struct | System.AttributeTargets.All, Inherited=false) System.Runtime.InteropServices.ComVisible(true)

The following example demonstrates the use of the , and the .

The non-standard function used in this example indicates whether the specified point is located inside the specified rectangle. In this example, the layout setting on the structure can be set to with no bearing on the end result.

`using System; using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential)] public struct Point { public int x; public int y; } [StructLayout(LayoutKind.Explicit)] public struct Rect { [FieldOffset(0)] public int left; [FieldOffset(4)] public int top; [FieldOffset(8)] public int right; [FieldOffset(12)] public int bottom; } class NativeCodeAPI { [DllImport("User32.dll")] public static extern bool PtInRect(ref Rect r, Point p); } public class StructLayoutTest { public static void Main() { Rect r; Point p1, p2; r.left = 0; r.right = 100; r.top = 0; r.bottom = 100; p1.x = 20; p1.y = 30; p2.x = 110; p2.y = 5; bool isInside1 = NativeCodeAPI.PtInRect(ref r, p1); bool isInside2 = NativeCodeAPI.PtInRect(ref r, p2); if(isInside1) Console.WriteLine("The first point is inside the rectangle."); else Console.WriteLine("The first point is outside the rectangle."); if(isInside2) Console.WriteLine("The second point is inside the rectangle."); else Console.WriteLine("The second point is outside the rectangle."); } }`

The output is

The first point is inside the rectangle.

The second point is outside the rectangle.

You can apply this attribute to classes or structures.

The common language runtime controls the physical layout of the data fields of a class or structure in managed memory. However, if you want to pass the type to unmanaged code, you can use the attribute to control the unmanaged layout of the type. Use the attribute with to force the members to be laid out sequentially in the order they appear. For blittable types, controls both the layout in managed memory and the layout in unmanaged memory. For non-blittable types, it controls the layout when the class or structure is marshaled to unmanaged code, but does not control the layout in managed memory. Use the attribute with to control the precise position of each data member. This affects both managed and unmanaged layout, for both blittable and non-blittable types. Using requires that you use the attribute to indicate the position of each field within the type.

C#, Visual Basic, and C++ compilers apply the layout value to structures by default. For classes, you must apply the value explicitly. The [<topic://cpgrfTypeLibraryImporterTlbimpexe>] also applies the attribute; it always applies the value when it imports a type library.

Lets you control the physical layout of the data fields of a class or structure.

- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  This constructor takes an underlying 16-bit integer that represents each enumeration member. The [<topic://cpgrfTypeLibraryImporterTlbimpexe>] uses this constructor.

  Initalizes a new instance of the class with the specified enumeration member.

  A 16-bit integer that represents one of the values that specifes how the class or structure should be arranged.
  0
- Constructor
  1.0.5000.0
  2.0.0.0
  4.0.0.0

  For readable code that is less prone to error, always use this constructor.

  Initalizes a new instance of the class with the specified enumeration member.

  One of the enumeration values that specifes how the class or structure should be arranged.
  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Runtime.InteropServices.CharSet

  If the CharSet field is set to , all string arguments are converted to Unicode characters (LPWSTR) before they are passed to the unmanaged implementation. If the field is set to , the strings are converted to ANSI strings (LPSTR). If the CharSet field is set to , the conversion is platform-dependent (ANSI on Windows 98 and Windows Me, and Unicode on later versions).

  Indicates whether string data fields within the class should be marshaled as LPWSTR or LPSTR by default.

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32

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

  The value of 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 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.

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

  0
- Field
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Int32

  This field must be equal or greater than the total size, in bytes, of the members of the class or structure. This field is primarily for compiler writers who want to extend the memory occupied by a structure for direct, unmanaged access. For example, you can use this field when working with unions that are not represented in metadata directly.

  Indicates the absolute size of the class or structure.

  0
- Property
  1.0.5000.0
  2.0.0.0
  4.0.0.0
  System.Runtime.InteropServices.LayoutKind

  A value that specifies how the target object is arranged.

  This property is read-only.

  Gets the value that specifies how the class or structure is arranged.

  0
0