Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

FieldOffsetAttribute Class

Indicates the physical position of fields within the unmanaged representation of a class or structure.

For a list of all members of this type, see FieldOffsetAttribute Members.

System.Object
   System.Attribute
      System.Runtime.InteropServices.FieldOffsetAttribute

[Visual Basic]
<AttributeUsage(AttributeTargets.Field)>
NotInheritable Public Class FieldOffsetAttribute
   Inherits Attribute
[C#]
[AttributeUsage(AttributeTargets.Field)]
public sealed class FieldOffsetAttribute : Attribute
[C++]
[AttributeUsage(AttributeTargets::Field)]
public __gc __sealed class FieldOffsetAttribute : public Attribute
[JScript]
public
   AttributeUsage(AttributeTargets.Field)
class FieldOffsetAttribute extends Attribute

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Remarks

You can apply this attribute to fields.

This attribute is used when System.Runtime.InteropServices.StructLayoutAttribute, with LayoutKind.Explicit passed to its constructor, is applied to a class or structure to specify the offset of each non- static (Shared in Visual Basic) or constant member within the unmanaged representation of that class or structure. The attribute has no affect on the managed layout of the members of the type.

Example

[Visual Basic, C#, C++] The following example demonstrates how to apply the FieldOffsetAttribute to members of a class with an explicit layout.

[Visual Basic] 
<StructLayout(LayoutKind.Explicit)> _
Public Class SYSTEM_INFO
    <FieldOffset(0)> Private OemId As System.UInt64
    <FieldOffset(4)> Private PageSize As System.UInt64
    <FieldOffset(16)> Private ActiveProcessorMask As System.UInt64
    <FieldOffset(20)> Private NumberOfProcessors As System.UInt64
    <FieldOffset(24)> Private ProcessorType As System.UInt64
End Class

[C#] 
[StructLayout(LayoutKind.Explicit)]
public class SYSTEM_INFO
{
[FieldOffset(0)] public ulong OemId;
[FieldOffset(4)] public ulong PageSize;
[FieldOffset(16)] public ulong ActiveProcessorMask;
[FieldOffset(20)] public ulong NumberOfProcessors;
[FieldOffset(24)] public ulong ProcessorType;
}

[C++] 
[StructLayout(LayoutKind::Explicit)]
public __gc class SYSTEM_INFO
{
public:
[FieldOffset(0)] UInt64 OemId;
[FieldOffset(4)] UInt64 PageSize;
[FieldOffset(16)] UInt64 ActiveProcessorMask;
[FieldOffset(20)] UInt64 NumberOfProcessors;
[FieldOffset(24)] UInt64 ProcessorType;
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Namespace: System.Runtime.InteropServices

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

Assembly: Mscorlib (in Mscorlib.dll)

See Also

FieldOffsetAttribute Members | System.Runtime.InteropServices Namespace | StructLayoutAttribute | LayoutKind

Show:
© 2015 Microsoft