RuntimeHelpers Class


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Provides a set of static methods and properties that provide support for compilers. This class cannot be inherited.

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


public static class RuntimeHelpers


Gets the offset, in bytes, to the data in the given string.


Ensures that the remaining stack space is large enough to execute the average .NET Framework function.

System_CAPS_pubmethodSystem_CAPS_staticEquals(Object, Object)

Determines whether the specified Object instances are considered equal.

System_CAPS_pubmethodSystem_CAPS_staticExecuteCodeWithGuaranteedCleanup(RuntimeHelpers.TryCode, RuntimeHelpers.CleanupCode, Object)

Executes code using a Delegate while using another Delegate to execute additional code in case of an exception.


Serves as a hash function for a particular object, and is suitable for use in algorithms and data structures that use hash codes, such as a hash table.


Boxes a value type.

System_CAPS_pubmethodSystem_CAPS_staticInitializeArray(Array, RuntimeFieldHandle)

Provides a fast way to initialize an array from data that is stored in a module.


Designates a body of code as a constrained execution region (CER).


Designates a body of code as a constrained execution region (CER) without performing any probing.


Provides a way for applications to dynamically prepare AppDomain event delegates.


Indicates that the specified delegate should be prepared for inclusion in a constrained execution region (CER).


Prepares a method for inclusion in a constrained execution region (CER).

System_CAPS_pubmethodSystem_CAPS_staticPrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[])

Prepares a method for inclusion in a constrained execution region (CER) with the specified instantiation.


Probes for a certain amount of stack space to ensure that a stack overflow cannot happen within a subsequent block of code (assuming that your code uses only a finite and moderate amount of stack space). We recommend that you use a constrained execution region (CER) instead of this method.


Runs a specified class constructor method.


Runs a specified module constructor method.

The following example shows how to reliably set handles by using the PrepareConstrainedRegions method. To reliably set a handle to a specified pre-existing handle, you must ensure that the allocation of the native handle and the subsequent recording of that handle within a SafeHandle object is atomic. Any failure between these operations (such as a thread abort or out-of-memory exception) will result in the native handle being leaked. You can use the PrepareConstrainedRegions method to make sure that the handle is not leaked.

struct MyStruct
    public IntPtr m_outputHandle;

sealed class MySafeHandle : SafeHandle
    // Called by P/Invoke when returning SafeHandles
    public MySafeHandle()
        : base(IntPtr.Zero, true)

    public MySafeHandle AllocateHandle()
        // Allocate SafeHandle first to avoid failure later.
        MySafeHandle sh = new MySafeHandle();

        try { }
            MyStruct myStruct = new MyStruct();
            NativeAllocateHandle(ref myStruct);

        return sh;

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1

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

Return to top