Not all variables will handle the value of null. Some of the variables that can be assigned null simply stored null as zero. Before, nullability is handled by implementing a try-catch block to catch nulls, assign a special variable (negative one for integers) to imply null, convert to any object to handle null, or even go as far as creating a class. There is now an easy way to enable any variable to handle nullability.
int? myInteger = null
To explain the above example, by placing a question mark (?) after the variable declaration, we are stating that this variable is nullable. If you have ever wondered: “What does that question mark after int”, or “What does that question mark after double” mean, now you know!
Variable nullability is especially useful in the case of database calls. A common situation where this is necessary is when the database call will return an integer value from a column that can be null. Everything works until a null row is returned and the project breaks at run time.
Next time you are making a call to database, try out this feature of C#.
This entry was posted on Monday, March 24th, 2008 at 6:59 am and is filed under C#. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.


Leave a Reply