By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,424 Members | 1,351 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,424 IT Pros & Developers. It's quick & easy.

How many nulls are there?

P: n/a
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.
Jul 2 '08 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Author wrote:
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.

--
J.
Jul 2 '08 #2

P: n/a
On Jul 2, 4:50*pm, Jeroen Mostert <jmost...@xs4all.nlwrote:
Author wrote:
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.

--
J.

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();
}
}
}
Jul 2 '08 #3

P: n/a
Author <gn********@gmail.comwrote:
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...

--
Jon Skeet - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon_skeet
C# in Depth: http://csharpindepth.com
Jul 2 '08 #4

P: n/a
>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...
Although to be fair, q and p look very much alike with some fonts.
Jul 2 '08 #5

P: n/a
On Jul 2, 5:10 pm, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Author <gnewsgr...@gmail.comwrote:
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...
// 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.
Jul 3 '08 #6

P: n/a
On Jul 2, 6:12 pm, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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...

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.

Jul 3 '08 #7

This discussion thread is closed

Replies have been disabled for this discussion.