Windows apps
Collapse the table of content
Expand the table of content
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::GetObjectValue Method (Object^)

 

Boxes a value type.

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

public:
static Object^ GetObjectValue(
	Object^ obj
)

Parameters

obj
Type: System::Object^

The value type to be boxed.

Return Value

Type: System::Object^

A boxed copy of obj if it is a value class; otherwise, obj itself.

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

This 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, instead of a copy of the class.

Compilers of dynamically typed languages can use this 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 a type object.

This method is used by compilers.

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

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Return to top
Show:
© 2017 Microsoft