O'Reilly logo

C# in a Nutshell by Peter Drayton, Ted Neward, Ben Albahari

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Name

GCHandle

Synopsis

This class is used when you need to pass a managed object to unmanaged code. To use this class, pass an instance of a .NET-managed type to the Alloc() method. The single-argument form of Alloc() creates the GCHandle with GCHandleType.Normal, which ensures that the object will not be freed by the garbage collector. (This means that some kind of user code must also call the Free() method in order to release the object.) Managed code can use the Target property to access the underlying object.

public struct GCHandle {
// Public Instance Properties
   public field bool IsAllocated{get; } 
   public field object Target{set; get; } 
// Public Static Methods
   public static method GCHandle Alloc(object value);  
   public static method GCHandle Alloc(object value, 
        GCHandleType type);  
   public static explicit operator method GCHandle(
        IntPtr value);  
   public static explicit operator method IntPtr(
        GCHandle value);  
// Public Instance Methods
   public method IntPtr AddrOfPinnedObject();  
   public method void Free();  
}

Hierarchy

System.ObjectSystem.ValueTypeGCHandle

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required