473,406 Members | 2,847 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,406 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 966
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
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.