Chris Ongsuco's Weblog
Information Technology, business, life, food...

null != null ???

Tuesday, August 02, 2005 4:55 AM
int? i = null;
object obj = "some string...";

obj = i;

if (obj == null)
{
MessageBox.Show("true...");
}
else {
MessageBox.Show("false...");
}


OK, why is it that obj == null is false? Is it because i is a value type and obj is an object type? I don't think it's because the variable i is a value type since this will defeat the purpose of nullable types....

  • Share This Post:
  • Share on Twitter
  • Share on Facebook
  • Share on Technorati

Feedback

# re: null != null ???

This unexpected behaviour also happens even if you declare the object as null to start with i.e.

object obj = null;

So there must be an incompatibility between nullable types (underlying value types) and casting them into an object type.

Interestingly, if you set obj = 1, and then preform the comparison,

(obj == i)

this too returns false!!

Some work required by the VS.NET team on this one. 8/2/2005 5:44 AM | Liam Westley

# re: null != null ???

You are seeing the correct and expected behavior. Why? You are working with a real instance of an object -- it is not a null object. Yes, this particular object's "value" is null, but it is an object and is thus not itself null. 8/2/2005 9:33 AM | Paul Wilson

# re: null != null ???

Paul has it right - there's absolutely nothing broken there.

A nullable object is exactly that - an object of type nullable. there's a few special things that happen behind the scenes whenever you use nullables. assigning null to a nullable actually causes an instantiation, but leaves it with it's default value (which is the HasValue() property returning false).

If you take your code, compile, bung it through reflector, and have a look at the reconstructed source, you can see better what is going on.

Nullable<int> nullable1 = new Nullable<int>();
object obj1 = "some string...";
obj1 = nullable1;
if (obj1 == null)
{
MessageBox.Show("true...");
}
else
{
MessageBox.Show("false...");
}
8/2/2005 9:43 AM | Geoff Appleby

# re: null != null ???

I've run into this "unexpected" behavior myself. It seems that nullables are valuetype structs (and hence ALWAYS exist) so in theory:

int? i = null; //i is not really null here.
object o = i; //again o isnt null here, its a Nullable<T> struct, boxed.

if(i==null)
MessageBox.Show("true");
else
MessageBox.Show("false");

Debug.WriteLineIf(i.HasValue,"i has a value");
//notice that since Nullable is a struct
//(valuetype) then setting i=null only sets the Nullable struct's VALUE to null... but
//there IS a value reference here. and you dont get a NullReferenceException at runtime

The semantics of Nullables converted to object can be a little confusing. Test/debug and you'll get it :-) 8/2/2005 3:38 PM | Eric Newton

# re: null != null ???

I was pretty straight forward there when I thought I could just compare a nullable type with "== null". Guess that answered my question. :-)

Thanks guys. 8/2/2005 11:45 PM | Chris Ongsuco

# re: null != null ???

Yep that all makes sense and it is a logical handling of the issue.

So now we have to check for either null, HasValue, and System.DBNull depending on the situation. Joy. 8/3/2005 4:44 AM | Liam Westley

# re: null != null ???

Is it because i is a value type? Or is it because i is black? 9/28/2005 11:29 AM | Ali G

Post a comment