Skip to content

Latest commit

 

History

History
41 lines (32 loc) · 1.97 KB

File metadata and controls

41 lines (32 loc) · 1.97 KB
title Boxing (C++/CX) | Microsoft Docs
ms.custom
ms.date 12/30/2016
ms.technology cpp-windows
ms.topic language-reference
ms.assetid edfb12fa-2a9b-42f6-bdac-d4d76cb8274e
author ghogen
ms.author ghogen
ms.workload
cplusplus

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)
Visual C++ Language Reference
Namespaces Reference