MorkaLork Development

Interesting stuff I've picked up over the years...

Nullable Types

2009-04-16 17:55:42 | 167 views | type data null nullable nulleable denoted questionmark


As you might know, a value type in C# can't be null. Well, you know wrong =O

A value type can be null just like a keyword can be a variable (see @ - Verbatim).

This code won't compile:

string s = null;
int i = null; //<--it will break here!

An int can't just be nullified, so you'll have to hack it! To null it, you have to use a special construct called a nullable type. A nullable type is denoted with a value type and a questionmark:

int? i = null; //yeah!
bool? b = null; //yeah!!!
double? d = null; //YEAH!!!
//'s all good...


Well, if you use a boolean named check that's supposed to control if a value is true or false, you might come to a situation where the value is undefined, then it would be a good thing to have the option null available.

Other then that, I got nothing...

Article comments

Feel free to comment this article using a facebook profile.

I'm using facebook accounts for identification since even akismet couldn't handle all the spam I receive every day.