How many nulls are there?

A

Author

I was browsing the MSDN class library for the Object class, and tested
its sample code.

If we have

Object o = null;
Object p = null;

Then

Object.ReferenceEquals(o, p);

will give me true. This seems to mean that o and p are both pointing
to the same object. Fair enough.

But, if we instantiate another Object q

Object q = new Object();
q = null;

Now,

Object.ReferenceEquals(o, q);

gives us false, although both o and q have a null value. This seems
to mean that o and q are pointing to different stuffs. The question
is, why when we do:

Object o = null;
Object p = null;

o and q seem to point to the same stuff?

I am not sure if I made my question clear.
 
J

Jeroen Mostert

Author said:
I was browsing the MSDN class library for the Object class, and tested
its sample code.

If we have

Object o = null;
Object p = null;

Then

Object.ReferenceEquals(o, p);

will give me true. This seems to mean that o and p are both pointing
to the same object. Fair enough.
No. Neither o nor p are pointing to any object. That's the point of null: to
allow a reference to refer to no object. The references have the same value
here, that much is true.
But, if we instantiate another Object q

Object q = new Object();
q = null;

Now,

Object.ReferenceEquals(o, q);

gives us false, although both o and q have a null value.

Really? Try giving a complete, working program that exhibits this behavior.
I couldn't.
 
A

Author

No. Neither o nor p are pointing to any object. That's the point of null:to
allow a reference to refer to no object. The references have the same value
here, that much is true.




Really? Try giving a complete, working program that exhibits this behavior.
I couldn't.


Try this and you'll see:

using System;

namespace ConsoleApplication1
{
class ObjectEqualsTest
{
public static void Main()
{
object o = null;
object p = null;
object q = new Object();

Console.WriteLine("o=null, p=null --> " +
Object.ReferenceEquals(o, p));
Console.WriteLine("p=null; q=new Object() --> " +
Object.ReferenceEquals(p, q));

p = q;
Console.WriteLine("p=q --> " + Object.ReferenceEquals(p,
q));

q = null;
Console.WriteLine("q = null; o=null; o and q --> " +
Object.ReferenceEquals(o, p));

Console.Read();
}
}
}
 
J

Jon Skeet [C# MVP]

Author said:
I was browsing the MSDN class library for the Object class, and tested
its sample code.

If we have

Object o = null;
Object p = null;

Then

Object.ReferenceEquals(o, p);

will give me true. This seems to mean that o and p are both pointing
to the same object. Fair enough.

As Jeroen said, neither of them are pointing to any object.
But, if we instantiate another Object q

Object q = new Object();
q = null;

Now,

Object.ReferenceEquals(o, q);

gives us false, although both o and q have a null value.

That's not what your code tests. Look at this statement carefully:

Console.WriteLine("q = null; o=null; o and q --> " +
Object.ReferenceEquals(o, p));

Look at what variables it's *claiming* to compare, then look at the
actual method arguments...
 
B

Ben Voigt [C++ MVP]

Now,
That's not what your code tests. Look at this statement carefully:

Console.WriteLine("q = null; o=null; o and q --> " +
Object.ReferenceEquals(o, p));

Look at what variables it's *claiming* to compare, then look at the
actual method arguments...

Although to be fair, q and p look very much alike with some fonts.
 
A

Author

As Jeroen said, neither of them are pointing to any object.




That's not what your code tests. Look at this statement carefully:

Console.WriteLine("q = null; o=null; o and q --> " +
Object.ReferenceEquals(o, p));

Look at what variables it's *claiming* to compare, then look at the
actual method arguments...

// blush for being fooled by the typos of mine.

This is probably one reason we should have more friendly variable
names such as variable1, number1, number2.
 
A

Author

Although to be fair, q and p look very much alike with some fonts.

Especially when one quite often sees the left as if right and sees the
right as if left, such as me. :) I need to *see* my p's and q's.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top