22

I have never used nullable types in my C# code. Now I have decided to change my coding practice by introducing nullable types in my code.

What are the major changes in the coding practices should be made while making a transition from normal datatypes to nullable data-types in case of application programming?

What are the areas that should be taken care of?

What are the points I should always watch out for?

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
user366312
  • 16,949
  • 65
  • 235
  • 452

6 Answers6

21

Don't use nullable types because they are a "cool new thing" you have discovered. Use them where they are applicable and genuinely useful.

There are overheads to using them, and if used incorrectly they will unnecessarily increase the complexity of your code.

You also have to be careful to avoid null dereferences, so they place additonal burden on programmers working with that code. (In some cases this is preferable to the cost of a workaround approach though!)

Jason Williams
  • 56,972
  • 11
  • 108
  • 137
  • This is never a good idea. Coz nobody know what changes are coming in the .net framework and may be that change is entirely based upon Nullable types. – user366312 Oct 11 '09 at 15:16
  • 5
    @JMSA: I don't understand what you're trying to say. – Jason Williams Oct 11 '09 at 17:53
  • 1
    +1. Nullable types are incredibly useful when you need them, but Jason's right about avoiding burdening callers with the need to check values vs. null all the time. Consider throwing an exception if null is not valid. – TrueWill Oct 11 '09 at 18:57
17

Nullable<T> is useful for when you need a possible invalid state for a value type, or if the data is being retrieved from a database that may contain a null value for the column. It is very common in some old FORTRAN code I am porting to C# for invalid values to be negative or 0, but this is troublesome especially when the values are used in mathematical functions. It is far more explicit to use Nullable<T> to show that possible invalid state.

It is worth mentioning that the following is the same:

Nullable<int> myNullableInt;
int? myNullableInt;
user7116
  • 63,008
  • 17
  • 141
  • 172
  • 11
    Tips: Don't forget, null value from database is represented as DBNull.Value by ADO.NET. Make sure to use DBNull.Value to communicate with DB instead of null of Nullable. – Ferry Meidianto Oct 11 '09 at 16:21
9

In addition I find the following property useful:

public bool? IsHappy { get; set; }

This allows me to have a tri-state boolean value: yes, no, not answered.

youwhut
  • 948
  • 3
  • 17
  • 37
6

A couple of other good ideas for using nullable types:

  • Don't forget the flexibility in syntax. Nullable<int> is the same as int?
  • Check for null (var.HasValue) and cast it to base type before using it as the base type.
LJM
  • 6,284
  • 7
  • 28
  • 30
  • 'cast it to base type before using it as the base type.' - I don't follow what you mean here... If you want the value, just use 'var.Value', or even 'var' if you've already checked it has a value. – nicodemus13 Mar 28 '12 at 10:41
  • 1
    Oh, I follow what you mean here; you mean when assigning it to a new variable of the underlying type? – nicodemus13 Mar 28 '12 at 10:51
  • Yes. I.e. always check to be sure your Nullable has as value before putting that value into an int. – LJM Mar 29 '12 at 13:39
3

They seem suitable for the starting value of some value type variables.

int? lastCodeReceived;

if (lastCodeReceived.HasValue)
{
    // At least one code has been received.
}
xyz
  • 27,223
  • 29
  • 105
  • 125
2

I rarely use nullable types. The only place I've use them is when I'm dealing with null types in the database.

Chuck Conway
  • 16,287
  • 11
  • 58
  • 101