Page 1 of 1

Nullable Values in C#

#1 sontek  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 8
  • View blog
  • Posts: 283
  • Joined: 13-September 01

Posted 25 January 2006 - 07:01 PM

To setup a value to be nullable you just place a question mark after its type:

int? count = null;



so now the compiler will know that you can test if that object is null (rather than setting it to a default value which can cause errors down the road).

Another cool feature in .NET 2.0 is the ?? operator, Which works a lot like the ternary operator but returns itself if the value is not null.

such as:
int? count = null;
int value = count ?? 0;



value is now 0. It would be the same as doing
int? count = null;
int value = count != null ? count : 0;




Hope this helps!

Is This A Good Question/Topic? 1
  • +

Page 1 of 1