LayoutKind Enum

Definition

Controls the layout of an object when exported to unmanaged code.

C#
public enum LayoutKind
C#
[System.Serializable]
public enum LayoutKind
C#
[System.Serializable]
[System.Runtime.InteropServices.ComVisible(true)]
public enum LayoutKind
Inheritance
LayoutKind
Attributes

Fields

Name Value Description
Sequential 0

The members of the object are laid out sequentially, in the order in which they appear when exported to unmanaged memory. The members are laid out according to the packing specified in Pack, and can be noncontiguous.

Explicit 2

The precise position of each member of an object in unmanaged memory is explicitly controlled, subject to the setting of the Pack field. Each member must use the FieldOffsetAttribute to indicate the position of that field within the type.

Auto 3

The runtime automatically chooses an appropriate layout for the members of an object in unmanaged memory. Objects defined with this enumeration member cannot be exposed outside of managed code. Attempting to do so generates an exception.

Examples

The following example shows the managed declaration of the PtInRect function, which checks whether a point lies within a rectangle, and defines a Point structure with Sequential layout and a Rect structure with Explicit layout.

C#
enum Bool
{
   False = 0,
   True
};
[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;
}

internal static class NativeMethods
{
   [DllImport("user32.dll", CallingConvention=CallingConvention.StdCall)]
   internal static extern Bool PtInRect(ref Rect r, Point p);
};

class TestApplication
{
   public static void Main()
   {
      try
      {
         Bool bPointInRect = 0;
         Rect myRect = new Rect();
         myRect.left = 10;
         myRect.right = 100;
         myRect.top = 10;
         myRect.bottom = 100;
         Point myPoint = new Point();
         myPoint.x = 50;
         myPoint.y = 50;
         bPointInRect = NativeMethods.PtInRect(ref myRect, myPoint);
         if(bPointInRect == Bool.True)
            Console.WriteLine("Point lies within the Rect");
         else
            Console.WriteLine("Point did not lie within the Rect");
      }
      catch(Exception e)
      {
         Console.WriteLine("Exception : " + e.Message);
      }
   }
}

Remarks

This enumeration is used with StructLayoutAttribute. The common language runtime uses the Auto layout value by default. To reduce layout-related problems associated with the Auto value, C#, Visual Basic, and C++ compilers specify Sequential layout for value types.

Important

The StructLayoutAttribute.Pack field controls the alignment of data fields, and thus affects the layout regardless of the LayoutKind value you specify. By default, the value of Pack is 0, which indicates the default packing size for the current platform. For example, when you use the Explicit layout value and specify field alignments on byte boundaries, you must set Pack to 1 to get the desired result.

Applies to

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 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.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0