Nullable Data Types
I just found a very easy way of using nullable data type in c#. While this may not be the freshest of news to all i though i should share it anyway.
Nullable data types allow you to either store null in a variable or just the value.
e.g.
Nullable<int> count;
if (count.HasValue)
{
someOtherVar = count.Value ;
}
if this was done without nullable types it would result in a null reference exception , but in this case the Has value would equate to false and the code would not execute.This is all fine when working with small sets of code ,but eventually if you start using this alot (especially with databases) this tends to get unsightly .So the cleaner version now.
int? test;
if(test != null)
{
someOtherVar = test;
}
As one can see this definitely cleans up the code , some people might complain that the Has value makes more sense , in this case it all comes down to personal preference as there is no advantages to either way.
2 Comments to “Nullable Data Types”
Leave a Reply








Nice post, I think the main reason MS had behind the nullable types were for when they’re being used as parameters into a method. Thats where the power of nullable types shine for me personally. But ofcourse each person will have their own “favourite” points.
Cheers!
I was going to post on the methods as it can get very interesting and dangerous as well . But writing long pieces of code without formating becomes quite ugly ,so i might just post a file with code in it .