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.
This topic has not yet been rated - Rate this topic

RuntimeHelpers.GetObjectValue Method

Boxes a value type.

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

public static Object GetObjectValue (
	Object obj
)
public static Object GetObjectValue (
	Object obj
)
public static function GetObjectValue (
	obj : Object
) : Object
Not applicable.

Parameters

obj

The value type to be boxed.

Return Value

Returns a boxed copy of obj if it is a value class; otherwise obj itself is returned.

Boxing a value type creates an object and performs a shallow copy of the fields of the specified value type into the new object.

The GetObjectValue method allows a value class to be manipulated as an object while it retains the aliasing behavior of a value class.

The return value depends on whether the value class is mutable or immutable:

  • If the value being assigned is a mutable value class, the method returns a shallow copy of the class, because value classes have copy semantics.

  • If the value being assigned is an immutable value class, the method returns the object itself, rather than a copy of the class.

Compilers of dynamically typed languages can use the GetObjectValue method to make sure that boxed value types work identically to unboxed value types. That is, boxed value types get cloned when you pass them around, and they are always passed by value. The compiler can call GetObjectValue to assign a value type to an object or to pass a value type as a parameter of type object.

The following code example demonstrates how to box a value class using the GetObjectValue method.

using System;
using System.Runtime.CompilerServices;

// Declare a value type.
struct Point2I
{
    public int x;
    public int y;
}

class Program
{

    static void Main(string[] args)
    {
        // Allocate an unboxed Point2I (not on the heap).
        Point2I pnt;
        pnt.x = 0;
        pnt.y = 0;

        // Box the value.  (Put it in the heap.)
        object objPntr = RuntimeHelpers.GetObjectValue(pnt);

    }
}

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 2.0, 1.0

XNA Framework

Supported in: 1.0
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2014 Microsoft. All rights reserved.