使用Platform::IBox<T>类型支持可空类型。
C++:
public ref class Class1 sealed
{
public:
Class1(){}
Platform::IBox<int>^ Multiply(Platform::IBox<int>^ a, Platform::IBox<int>^ b)
{
if(a == nullptr || b == nullptr)
return nullptr;
else
return ref new Platform::Box<int>(a->Value * b->Value);
}
};
C#调用:
Class1 obj = new BoxingDemo.Class1();
int? a = null;
int? b = 5;
var result = obj.Multiply(a,b); //result = null