Skip to content

Latest commit

 

History

History
38 lines (27 loc) · 1.75 KB

boxing-c-cx.md

File metadata and controls

38 lines (27 loc) · 1.75 KB
description title ms.date ms.assetid
Learn more about: Boxing (C++/CX)
Boxing (C++/CX)
12/30/2016
edfb12fa-2a9b-42f6-bdac-d4d76cb8274e

Boxing (C++/CX)

Boxing is wrapping a value type variable such as Windows::Foundation::DateTime—or a fundamental scalar type such as int—in a ref class when the variable is passed to a method that takes Platform::Object^ as its input type.

Passing a value type to an Object^ parameter

Although you don't have to explicitly box a variable to pass it to a method parameter of type Platform::Object^, you do have to explicitly cast back to the original type when you retrieve values that have been previously boxed.

[!code-cppcx_boxing#01]

Using Platform::IBox<T> to support nullable value types

C# and Visual Basic support the concept of nullable value types. In C++/CX, you can use the Platform::IBox<T> type to expose public methods that support nullable value type parameters. The following example shows a C++/CX public method that returns null when a C# caller passes null for one of the arguments.

[!code-cppcx_boxing#02]

In a C# XAML client, you can consume it like this:

// C# client code
BoxingDemo.Class1 obj = new BoxingDemo.Class1();
int? a = null;
int? b = 5;
var result = obj.Multiply(a, b); //result = null

See also

Type System (C++/CX)
Casting (C++/CX)
C++/CX Language Reference
Namespaces Reference