473,244 Members | 1,322 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,244 software developers and data experts.

How many nulls are there?

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
6 963
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
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
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
>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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dan Perlman | last post by:
From: "Dan Perlman" <dan@dpci.NOSPAM.us> Subject: ODBC creating nulls? Date: Friday, July 09, 2004 10:43 AM Hi, Below is my VB6 code that writes data from an Access 2000 table to a PG table....
3
by: aaj | last post by:
Hi I am probably going to regret asking this because I'm sure you are going to tell me my design is bad 8-) ah well we all have to learn.... anyway I often use Nulls as a marker to see if...
0
by: Rhino | last post by:
I am working with SQL Functions in DB2 for Windows/Linux/UNIX (V8.2.1) and am having a problem setting input parameters for SQL Functions to null in the Development Center. My simple function,...
1
by: PST | last post by:
Here's a problem I'm trying to deal with: I'm working on a Frontpage 2000 website for a boat handicapping system, built in Access 97. What I'm trying to accomplish is: The user enters a...
13
by: jt | last post by:
I can't seem to find a way to concatenate strings that have nulls within the string. I have a string that I need another string that has nulls in it and what to append the 2nd string, 3 string...
3
by: Simon | last post by:
Hi all, Do you think the best way to avoid the problems of nulls in the database is just to provide default values via the db schema? Alternatively, is it better to allow nulls, seeing as the...
4
by: Edmund Dengler | last post by:
Howdy all! Just checking on whether this is the expected behaviour. I am transferring data from multiple databases to single one, and I want to ensure that I only have unique rows for some...
3
by: Bob Stearns | last post by:
I am creating an index on a column which is 40% NULLS. The process seems to run forever, though a count of the number of values runs in milliseconds. This leads to the subject question: is there a...
1
by: =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post by:
Hello, I've eliminated the bulk of code, this should be sufficient: byte fromEncrypt; string sDecryptedString; //Read the data out of the crypto stream. csDecrypt.Read(fromEncrypt, 0,...
8
by: shira | last post by:
I have done a fair bit of searching, but haven't yet been able to find an explanation as to why one would set "ignore nulls" to "yes" when creating an index. I understand what it does (I think),...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.