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.