This documentation is archived and is not being maintained.

FixedAddressValueTypeAttribute Class

Fixes the address of a static value type field throughout its lifetime. This class cannot be inherited.


Namespace:  System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)

public sealed class FixedAddressValueTypeAttribute : Attribute

The FixedAddressValueTypeAttribute type exposes the following members.

Public methodFixedAddressValueTypeAttributeInitializes a new instance of the FixedAddressValueTypeAttribute class.

Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)

Public methodEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)

Use the FixedAddressValueTypeAttribute attribute to mark static value types as pinned upon creation.

This attribute is used by the Microsoft Visual C++ compiler.

Static value type fields are created as boxed objects. This means that their address can change as garbage collection is performed. When you apply this attribute to a static value type, its address remains constant during its lifetime.

The following example illustrates the use of the FixedAddressValueTypeAttribute attribute to pin a static field in memory. It defines an Age structure and initializes two classes that have static fields of type Age. The second class applies FixedAddressValueTypeAttribute to pin the field's address. A number of memory allocations are made before and after these two objects are instantiated and the garbage collector is invoked. The output from the example shows that although the address of the first Age field has changed after garbage collection, the address of the field to which FixedAddressValueTypeAttribute is applied has not.

using System;
using System.Runtime.CompilerServices;

public struct Age {
   public int years;
   public int months;

public class FreeClass
   public static Age FreeAge;

   public static unsafe IntPtr AddressOfFreeAge()
      fixed (Age* pointer = &FreeAge) 
      { return (IntPtr) pointer; } 

public class FixedClass
   public static Age FixedAge;

   public static unsafe IntPtr AddressOfFixedAge()
      fixed (Age* pointer = &FixedAge) 
      { return (IntPtr) pointer; } 

public class Example
   public static void Main()

      // Get addresses of static Age fields.
      IntPtr freePtr1 = FreeClass.AddressOfFreeAge();

      IntPtr fixedPtr1 = FixedClass.AddressOfFixedAge();

      // Garbage collection.

      // Get addresses of static Age fields after garbage collection.
      IntPtr freePtr2 = FreeClass.AddressOfFreeAge();
      IntPtr fixedPtr2 = FixedClass.AddressOfFixedAge();

      // Display addresses before and after garbage collection
      Console.WriteLine("Normal static: {0} -> {1}", freePtr1, freePtr2);
      Console.WriteLine("Pinned static:  {0} -> {1}", fixedPtr1, fixedPtr2);  

   // Allocate memory for 100,000 objects.
   static public void AllocateMemory()
      for (int ctr = 0; ctr <= 100000; ctr++)
         object o = new object();      
// The example displays output similar to the following:
//       Normal static: 19932420 -> 19863704
//       Pinned static:  19985508 -> 19985508

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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