« Don't Miss Out! | Main | Wow, Check it out! »
September 08, 2006
Nullable Types ?!?!?!?!?!??!?!?!
My blog has moved. Please go to: http://jstawski.com/archive/2006/09/08/Nullable-Types-_3F0021003F0021003F0021003F0021003F0021003F003F0021003F0021003F002100_.aspx
Today I learned something new. Today I learned this new feature of .net 2.0 and that feature is Nullable Types. What are they? Basically they are a typed object of a primitive/value type that can hold a value or null/nothing.
What's the difference between those types and the primitive ones? The only difference from a usefullness perspective is that you can have primitive values with null rather than a predefined value. Do I think they are usefull? nope, I don't. Do I think they are going to continue on till death do us apart? No, I don't.
What's the difference of setting an integer to a predifined "null" value like -1 rather than null? None, whatsover. The only time I see when it will be usefull is if the integer type can be any of the values from integer.min to integer.max. In that case there is no real value to set as a predefined "null". Is that scenario possible? Yes, it is. Is that scenario a normal ocurrence? Not in my experience.
This thing gets more interesting. What is a boolean? A boolean is a primitive type that can only hold 2 values: True or False. So what is a Nullable Boolean? In simple terms: an oxymoron. If by definition a boolean can have 2 values: true or false, so how can a boolean be null? It can't, because if it is null then it's not a boolean. So what is the conclusion to this? Microsoft used the wrong term for the booleans. Did you ever wonder why SQL Server (just to mention one DB) doesn't have a boolean type? What, MSSQL doesn't have a boolean type?!?!?!?!??!!?!? Nope, it doesn't. It has types that can represent a boolean, but they are not booleans. What are those types? Well any type that can hold at least 2 values: int, bit, varchar(1), char(1), etc, etc. The one that resembles more to the boolean is the bit with 2 possible values: 0, 1; but as any other field in the db, it can contain nulls and therefore it makes it 3 possible values.
Will I ever use this nullable types? probably not, but the nullable int might come in handy.
Too much talk, too little code, so here's how to use nullable types in C# and VB.net:
int? x = null;
bool? flag = null; //Oxymoron :)
//Or
System.Nullable<int> x = null;
System.Nullable<bool> flag = null;
Dim X as Nullable(Of Integer)
Dim flag as Nullable(Of Boolean)
As always, happy programming!!
07:06 PM | Permalink
Comments
Nullable booleans are great. You can declare them without knowing yet what the value will be. e.g. I want to declare blnIsSheMarried, there is definitely going to be a true or false answer but at this time we dont know either way.
Get with the program dude 8P
Posted by: UltimoNails | Jul 30, 2007 10:01:27 AM
Nullable booleans are great. You can declare them without knowing yet what the value will be. e.g. I want to declare blnIsSheMarried, there is definitely going to be a true or false answer but at this time we dont know either way.
Get with the program dude 8P
Posted by: UltimoNails | Jul 30, 2007 10:01:56 AM
Nullable booleans are great. You can declare them without knowing yet what the value will be. e.g. I want to declare blnIsSheMarried, there is definitely going to be a true or false answer but at this time we dont know either way.
Get with the program dude 8P
Posted by: UltimoNails | Jul 30, 2007 10:02:01 AM
this article makes no sense at all.
Posted by: Jon | Jan 28, 2009 1:23:40 PM
Nullable booleans can be useful as UltimoNails said.
Your following statement does not make sense: "What's the difference of setting an integer to a predifined "null" value like -1 rather than null? None, whatsover."
The whole point of null is that it is NOT -1, it isn't anything at all. Therefore you can check your value to make sure it isn't null, avoiding the use of using false data incalculations. Sure, you could check for -1 as well... but what if you calculate your value and it ends up being -1? You just discarded perfectly good information. Therefore, as good coding practice, nullible primitives can be useful when used correctly.
Posted by: Daemon | Mar 31, 2009 11:53:26 AM
The comments to this entry are closed.