Call c# from c++: how to pass nullptr to DateTime?
In a c# assembly, I got a function taking a nullable DateTime as parameter:
public void 开发者_Python百科DoSomething(DateTime? timestamp);
Now I want to call this method from c++/cli:
MyClass->DoSomething(nullptr);
This will not compile. Instead the c++ compiler will print an error message nullptr can not be converted to System::Nullable.
So how do I pass nullptr from c++ to a nullable DateTime?
MyClass->DoSomething(Nullable<DateTime>());
How to use Nullable types in c++/cli?
Nullable
is a value type and C++/CLI doesn’t provide compile-time magic for it. You need to go the explicit route:
System::Nullable<System::DateTime> dtnull;
MyClass->DoSomething(dtnull);
Of course, you can also use a temporary here:
MyClass->DoSomething(System::Nullable<System::DateTime>());
精彩评论