Monday, April 02, 2007

Nullable in C++/CLI

I was recently working on a C++/CLI project (which I only rarely do, as you'll soon find out)where there was a need to have a method returning a nullable DateTime.

I look up Nullable in the help, I see "ref class Nullable", think that the following will do it.

Nullable<DateTime>^ GetStuff();

I implement the thing and am a happy camper until I try to use it from a C# project. The intellisense says ValueType GetStuff(), WTF !?!

I take a look once again and find out that Nullable<T> is "public value class", so the correct incantation should have been:

Nullable<DateTime> GetStuff();

oh, and to return a null value you just do:

return Nullable<DateTime>();

(Anyone else finds the C# ? syntax for nullable types more easily digested?)

1 comment:

Biby Cletus said...
This comment has been removed by a blog administrator.