Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

RuntimeHelpers Class

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

System.Object
  System.Runtime.CompilerServices.RuntimeHelpers

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

public static class RuntimeHelpers

The RuntimeHelpers type exposes the following members.

  NameDescription
Public propertyStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsOffsetToStringDataGets the offset, in bytes, to the data in the given string.
Top

  NameDescription
Public methodStatic memberEnsureSufficientExecutionStackEnsures that the remaining stack space is large enough to execute the average .NET Framework function.
Public methodStatic memberEquals(Object, Object)Determines whether the specified Object instances are considered equal.
Public methodStatic memberExecuteCodeWithGuaranteedCleanupExecutes code using a Delegate while using another Delegate to execute additional code in case of an exception.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCode(Object)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.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetObjectValueBoxes a value type.
Public methodStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsInitializeArrayProvides a fast way to initialize an array from data that is stored in a module.
Public methodStatic memberPrepareConstrainedRegionsDesignates a body of code as a constrained execution region (CER).
Public methodStatic memberPrepareConstrainedRegionsNoOPDesignates a body of code as a constrained execution region (CER) without performing any probing.
Public methodStatic memberPrepareContractedDelegateProvides a way for applications to dynamically prepare AppDomain event delegates.
Public methodStatic memberPrepareDelegateIndicates that the specified delegate should be prepared for inclusion in a constrained execution region (CER).
Public methodStatic memberPrepareMethod(RuntimeMethodHandle)Prepares a method for inclusion in a constrained execution region (CER).
Public methodStatic memberPrepareMethod(RuntimeMethodHandle, RuntimeTypeHandle[])Prepares a method for inclusion in a constrained execution region (CER) with the specified instantiation.
Public methodStatic memberProbeForSufficientStackProbes 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.
Public methodStatic memberSupported by Portable Class LibrarySupported in .NET for Windows Store appsRunClassConstructorRuns a specified class constructor method.
Public methodStatic memberRunModuleConstructorRuns a specified module constructor method.
Top

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.

    [StructLayout(LayoutKind.Sequential)]
    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();

            RuntimeHelpers.PrepareConstrainedRegions();
            try { }
            finally
            {
                MyStruct myStruct = new MyStruct();
                NativeAllocateHandle(ref myStruct);
                sh.SetHandle(myStruct.m_outputHandle);
            }

            return sh;
        }

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

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.
Show:
© 2014 Microsoft