Export (0) Print
Expand All
This topic has not yet been rated - Rate this topic

5.2.3 Unboxing

Visual Studio .NET 2003

A boxed object of a __value class can be unboxed by using dynamic_cast (Section 7.5.1), or __try_cast (Section 7.5.2) to obtain a __gc pointer to the object that is stored in the "box" on the common language runtime heap. The __gc pointer can then be dereferenced to obtain a copy of the object of the __value class.

Example

// __unboxing.cpp
// compile with: /clr
#using <mscorlib.dll>
using namespace System;
__value struct V { int i; };

int main() {
   V v = {10};
   Object* o = __box(v);   // copy value to runtime heap
   V v2 = *dynamic_cast<__box V*>(o);   // copy back from runtime heap
}
Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft. All rights reserved.