StructLayout
attributeThe StructLayout
attribute is used to specify the layout of fields for the struct.
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct)] public class StructLayoutAttribute: System.Attribute { public StructLayoutAttribute(LayoutKind kind) {…} public CharSet CharSet; public int Pack; public LayoutKind StructLayoutKind { get {…} } }
The StructLayout
attribute has the following behaviors:
CharSet
named parameter indicates the default character set for containing char
and string
types. The default is CharSet.Auto
.Pack
named parameter indicates the packing size, in bytes. The packing size must be a power of two. The default packing size is 4.If LayoutKind.Explicit
is specified, then every field in the struct must have the StructOffset
attribute. If LayoutKind.Explicit
is not specified, then use of the StructOffset
attribute is prohibited.