harriyott.com

Thursday, September 27, 2007

Nullable int in C#

I was looking at somebody's code today (I've no idea whose), and I came across a line something like the following:

int? thing = GetSomethingOrOther();

Looks okay. Yup. Oh, hang on, what's that? There's a question mark after the int! How could that compile? Maybe it's an encoding issue or something. No, seems like a real one. Better google it. Ah, it's a nullable int. Fancy that. Well I never.

4 Comments:

Sam said...

You should check out "??", its even better...

int? a = null;

int b = a ?? 0;

September 27, 2007 8:16 PM  
Simon said...

Oddly, I came across ?? two years ago. I was apparently quite impressed with it then. I don't know why I didn't discover int? sooner. Thanks for commenting.

September 28, 2007 8:00 AM  
Colin Angus Mackay said...

Nullable types are a new addition to the C# 2.0 compiler. I quite like them.

September 28, 2007 11:30 AM  
Anonymous said...

Yes "new" as in about 2-3 years old.. LOL

October 01, 2007 11:16 AM  

Post a Comment

Links to this post:

Create a Link

<< Home