On Null or Why Empty Strings are not Same as Null

Null is an important, but sometimes hard concept. What’s the difference between an empty string and a null string? One of my first Stack Overflow questions was (NOT) NULL for NVARCHAR columns.

Some people claim that using null is always wrong or is some kind of voodoo.

This time Mike is wrong. There is a place for null, including Nullable<bool> and Nullable<int>.

Posted in C#, SQL on 2013-01-07 | Tagged

Null Handling with Extension Methods

Often we cannot be sure if a parameter passed in to a function has a value. To write error safe code all those parameters have to be checked for null and handled. We have the ?? coalesce operator to help, but still it can be quite a lot of code. Through the use of extension methods a lot of cases can be handled. As extension methods are in reality static methods of another class, they work even if the reference is null. This can be utilized to write utility methods for various cases. In this post I’ll show two cases, one is an extension that returns a default value for an XML attribute value if the attribute or even the element is missing. The other one handles the common case when a null sequence reference passed should be handled as an empty sequence.

Software Development is a Job – Coding is a Passion

I'm Anders Abel, an independent systems architect and developer in Stockholm, Sweden.

profile for Anders Abel at Stack Overflow, Q&A for professional and enthusiast programmers

Code for most posts is available on my GitHub account.

Popular Posts

Archives

Series

Powered by WordPress with the Passion for Coding theme.