Attention: We are retiring the ASP.NET Community Blogs. Learn more >

bool? res = count ?? 0 >= 0;

In today's presentation, Anders introduced some new syntax (to me) for nullable types. It goes like this: if you have a value variable (struct or simpler) that could get null values, you declare it like this:

int? intThatCouldBeNull;

So that later you could ask:

if (intThatCouldBeNull == null) ...

And we also get a coalesce operator:

int res = intThatCouldBeNull ?? 0;

This will assign 0 to res only if intThatCouldBeNull is indeed null. One caveat: if you compare two nulls the result is true. I would've expect the result to be null also, but they gave me good reasons for not being so. The debate is open though...

No Comments