2014-02-27

Comparing two null objects

Maybe it is my experience with databases but I have always thought that whenever you compare something with null it is always false. Obviously there are some exceptions.

One of the more common cases where you compare something with null and expect it to be true is if you compare an object with null. But what about strings? If you compare two string variables and both happens to be null what is the result. Personally I was a little surprised at first that string.Compare(null, null) returns zero. Probably because I'm thinking that the logic would be to return non-zero if either first or second argument is null. But that is probably my database brain thinking. It is actually much more convenient that comparing two null objects (using string.Compare or object.Equals) means the two objects are equal.

No comments:

Post a Comment