Microsoft Knowledge Base Email Alertz

How to control the physical layout of the data fields in the .NET Framework 2.0

Search KbAlertz

Advanced Search

Receive Microsoft Knowledge Base articles by E-Mail?

Every night we scan the Microsoft Knowledge Base. If technologies you're interested in are updated, we'll send you an e-mail. You only get one e-mail a day, and only when new articles are added.

Click here to create a
FREE account
Already have an account?
[Click here to Login]











Microsoft Knowledge Base Article

This article contents is Microsoft Copyrighted material.
©2005-©2007 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks

Article ID: 922785 - Last Review: November 22, 2007 - Revision: 1.4

How to control the physical layout of the data fields in the .NET Framework 2.0

INTRODUCTION

You can use the System.Runtime.InteropServices namespace and the StructLayoutAttribute class to control the physical layout of the data fields in the Microsoft .NET Framework 2.0. The target objects for these data fields are classes and structures. Typically, the common language runtime controls the physical layout of the data fields of a class or of a structure in managed memory. When you pass managed objects as arguments to unmanaged code, the system creates unmanaged representations of the objects. The StructLayoutAttribute class can control these unmanaged representations. Such control is necessary if the unmanaged code expects a specific layout or packing size. A StructLayoutAttribute constructor that uses the LayoutKind value as its parameter is used in the following example.
StructLayoutAttribute (System.Runtime.InteropServices.LayoutKind)
This code constructs and initializes a new instance of the StructLayoutAttribute class that uses the specified LayoutKind value.

The LayoutKind value Sequential is used to force the members of the structure to be laid out sequentially in the order that they appear in. The System.Runtime.InteropServices.StructLayoutAttribute.Pack field determines the memory alignment of data fields of a target object.

The value of the System.Runtime.InteropServices.StructLayoutAttribute.Pack field 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 operating system. The default value is defined according to the implementation.

MORE INFORMATION

The Visual Basic sample code in this section uses the sizeof operator to calculate the size of the following structure.
Structure TestDByte
		Dim a As Double
		Dim b As Byte
	End Structure
When you run the following sample program, the program calculates the size of this structure as 16.
using System;
using System.Runtime.InteropServices;

struct TestDByte
{
    public double a;
    public byte b;
}

class Test
{
unsafe static void Main()
{
TestDByte tdb;
Console.WriteLine( "Size: " + sizeof(tdb) );

}
}
If you do not use the StructLayoutAttribute.Pack field, the program shows the size of the structure as 16.

The following example reduces the structure size by using the StructLayoutAttribute.Pack field.
using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential, Pack=1)]
struct TestDByte
{
    public double a;
    public byte b;
}

class Test
{
unsafe static void Main()
{
TestDByte tdb;
Console.WriteLine( "Size: " + sizeof(tdb) );

}
}
The following example reduces the structure size to 9.
[StructLayout(LayoutKind.Sequential, Pack=1)]
The value 9 is equal to the sum of all the fields in the structure.

REFERENCES

For more information about how to use the StructLayoutattribute class from the .NET Framework, visit the following Microsoft Web site:
http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(vs.71).aspx (http://msdn2.microsoft.com/en-us/library/system.runtime.interopservices.structlayoutattribute(vs.71).aspx)

APPLIES TO
  • Microsoft .NET Framework 2.0
  • Microsoft .NET Compact Framework 2.0
  • Microsoft .NET Framework 2.0 Software Development Kit
Keywords: 
kbprogramming kbsamplecode kbtshoot kbapi kbinfo KB922785
       

Community Feedback System

Very often, it takes hours to solve a problem. Very often, you've looked high and low, and have tried a lot of solutions. When you finally found it, chances are, it was because someone else helped you. Here's your chance to give back. Use our community feedback tool to let others know what worked for you and what didn't.

Please also understand that the community feedback system is not warranted to be correct, it's simply a system that we've built to let people try and help each other. If something in a feedback response doesn't make sense to you, or you're not comfortable making changes that the feedback talks about (like registry edits), please consult a professional.

Thank you for using kbAlertz.com Feedback System.

-- Scott Cate