473,385 Members | 1,356 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,385 software developers and data experts.

Can someone explain this to me?

object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn't the boxed object use the Equals method of the object it
contains?
Aug 7 '06 #1
14 1343
Peter,

The reason that it throws in the first one is that you are performing a
reference comparison. The boxing has created two separate objects on the
heap which have the same boxed value.

In the second example, the implementation of Equals must check the
values in the boxed values (as opposed to ==/!=).

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Peter Morris [Droopy eyes software]" <pe**@droopyeyes.no.com.spamwrote in
message news:OQ**************@TK2MSFTNGP04.phx.gbl...
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn't the boxed object use the Equals method of the object it
contains?

Aug 7 '06 #2

Peter Morris [Droopy eyes software] wrote:
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn't the boxed object use the Equals method of the object it
contains?
In the first example, the code is checking to see if the object
references are the same (i.e. do the variables refer to the same
object?), which they are not. In the second example, the code is
checking to see if the object values are the same, which they are.

John

Aug 7 '06 #3
Peter Morris [Droopy eyes software] <pe**@droopyeyes.no.com.spam>
wrote:
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn't the boxed object use the Equals method of the object it
contains?
Unless it is overloaded, the == operator just compares object
references for reference types. Here, you've got references to two
different objects, so a != b. However, they have value equality, so
a.Equals(b).

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 7 '06 #4
The first comparison performs reference comparison (two distinct objects
with same value underneath) whereas the second one forces a value comarison.

"Peter Morris [Droopy eyes software]" <pe**@droopyeyes.no.com.spamwrote in
message news:OQ**************@TK2MSFTNGP04.phx.gbl...
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown? I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");

but why doesn't the boxed object use the Equals method of the object it
contains?
Aug 7 '06 #5
Peter Morris [Droopy eyes software] wrote:
object a = 5;
object b = 5;
if (a != b)
throw new InvalidOperationException("a != b");

Why is the exception thrown?
The object references are checked.
I guessed it is something to do with boxing
because the following does not throw an exception

object a = 5;
object b = 5;
if (!a.Equals(b))
throw new InvalidOperationException("a != b");
The object values are checked.
>
but why doesn't the boxed object use the Equals method of the object it
contains?

Aug 7 '06 #6
Unless it is overloaded, the == operator just compares object
references for reference types
But the dotnet way of comparing objects for new class types is to override
Equals right? So why doesn't == use Equals?

5 == (4 + 1) are not the same object reference, so surely the == is using
5.Equals(4 + 1)?
Aug 7 '06 #7
Peter Morris [Droopy eyes software] <pe**@droopyeyes.no.com.spam>
wrote:
Unless it is overloaded, the == operator just compares object
references for reference types

But the dotnet way of comparing objects for new class types is to override
Equals right?
If you call Equals, yes.
So why doesn't == use Equals?
Because == (unless it's overloaded) just compares references. The ==
operator is overloaded for some but far from all types.
5 == (4 + 1) are not the same object reference, so surely the == is using
5.Equals(4 + 1)?
No, because the == operator isn't overloaded on the boxed Int32 type.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 7 '06 #8
I'm not impressed at all to be honest. To implement "Equals" I have to do
two things?

1) Implement Equals
2) Override ==

I really don't like it. Two things are either equal or they are not. It
makes no sense to say A equals B but B does not equal A, and along these
lines I therefore state that A == B should always return the same as
A.Equals(B)

At the moment it's a bit too much like a human response:

Question: Does A equal B?
Answer: It depends who you ask


Aug 8 '06 #9
I appreciate the postings from everyone, thanks :-)
Aug 8 '06 #10
Peter Morris [Droopy eyes software] wrote:
I'm not impressed at all to be honest. To implement "Equals" I have to do
two things?

1) Implement Equals
2) Override ==
No. To implement Equals, you just have to implement Equals.

If you want to make it so that clients get value equality from ==
rather than reference equality, you also need to overload (not
override) ==. They're different things, and it often makes sense to do
one but not the other.
I really don't like it. Two things are either equal or they are not.
No - two references are either unrelated, refer to equal objects, or
refer to the same object.
It makes no sense to say A equals B but B does not equal A, and along these
lines I therefore state that A == B should always return the same as
A.Equals(B)
The problem is that A and B are references. A.Equals(B) is asking
whether the objects referred to by A and B are equal. A==B is (usually)
asking whether A and B are equal *in themselves*.
At the moment it's a bit too much like a human response:

Question: Does A equal B?
Answer: It depends who you ask
Suppose I go to see a production of "Sunday in the Park with George" in
London. (I wish I had the time...) Now suppose a friend of mine sees a
production of "Sunday in the Park with George" in Broadway.

Did we see the same show? Well, sort of - it was the same musical, but
different productions. (You could take the analogy further - what about
if we both saw it in London, but on different nights?)

Whether you like the behaviour or not, that *is* the behaviour of
C#/.NET.

Jon

Aug 8 '06 #11
Did we see the same show? Well, sort of - it was the same musical, but
different productions. (You could take the analogy further - what about
if we both saw it in London, but on different nights?)

But once you have decided whether or not you saw the same show I should get
the same answer no matter how I ask the question.

Sure it is the default behaviour of C# but that doesn't mean I have to like
it or think it is correct, but I do have to keep it in mind! Whenever
passing "object" as a parameter I should use Equals.
Thanks

Pete
Aug 8 '06 #12
Peter Morris [Droopy eyes software] wrote:
Did we see the same show? Well, sort of - it was the same musical, but
different productions. (You could take the analogy further - what about
if we both saw it in London, but on different nights?)


But once you have decided whether or not you saw the same show I should get
the same answer no matter how I ask the question.
No - if you ask "Did we see the same musical" it would be reasonable to
say "Yes". If you ask "Did we see the same performance" it would be
reasonable to say "No". Here, a performance is a sort of reference to
an instance of a production - two performances can be of the same
musical, but be different performances.
Sure it is the default behaviour of C# but that doesn't mean I have to like
it or think it is correct, but I do have to keep it in mind! Whenever
passing "object" as a parameter I should use Equals.
Assuming you want value equality rather than reference equality, yes.

Jon

Aug 8 '06 #13
This all reminds me of an old joke:

"No, no, Sally, I said 'All men are created equal,' NOT 'all men are made
the same!'

--
HTH,

Kevin Spencer
Microsoft MVP
Chicken Salad Surgery

Expect the unaccepted.

"Jon Skeet [C# MVP]" <sk***@pobox.comwrote in message
news:11*********************@p79g2000cwp.googlegro ups.com...
Peter Morris [Droopy eyes software] wrote:
Did we see the same show? Well, sort of - it was the same musical, but
different productions. (You could take the analogy further - what about
if we both saw it in London, but on different nights?)


But once you have decided whether or not you saw the same show I should
get
the same answer no matter how I ask the question.

No - if you ask "Did we see the same musical" it would be reasonable to
say "Yes". If you ask "Did we see the same performance" it would be
reasonable to say "No". Here, a performance is a sort of reference to
an instance of a production - two performances can be of the same
musical, but be different performances.
>Sure it is the default behaviour of C# but that doesn't mean I have to
like
it or think it is correct, but I do have to keep it in mind! Whenever
passing "object" as a parameter I should use Equals.

Assuming you want value equality rather than reference equality, yes.

Jon

Aug 8 '06 #14
"No, no, Sally, I said 'All men are created equal,' NOT 'all men are made
the same!'
:-)
Aug 8 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Maciej Nadolski | last post by:
Hi! I can`t understand what php wants from me:( So: Cannot send session cache limiter - headers already sent (output started at /home/krecik/public_html/silnik.php:208) in...
6
by: Ken Varn | last post by:
Sometimes when I try to close my managed C++ application, the following dialog displays in Win 2000 Pro: The title of the dialog is "Server Busy". The message is "This action cannot be completed...
8
by: Peter Foti | last post by:
I'm working on a site and the client said he thinks "the main title should be around 28pt and the subtitle probably 18pt". I know pt's are bad, and I want to convince him that he should NOT be...
5
by: J Allen Horner | last post by:
I'm just starting to learn C, and the tutorial I'm reading uses this code as an example: ----------- #include <stdio.h> #define MAX 10 int a; int rand_seed=10;
5
by: klj_mcsd | last post by:
Let's say you set txtlastname1.visible = true Then DirectCast(Page.FindControl("txtLastName1"), TextBox).Visible = False Why when you check txtlastname1.visible it is equal to True?
4
by: Richard L Rosenheim | last post by:
Using Visual Studio 2003 running under Windows XP Pro w/SP 1, when I create a new form, the form's title bar has the Windows 2000 look. Yet, in the various Microsoft demos, the form's title bar...
3
by: Jan Nielsen | last post by:
Hi I am working with rowfilters in dataviews. I would like to filter for empty fields (= null value in the database) I found this sentence on msdn: **************** To return only those columns...
1
by: Simon Windsor | last post by:
Hi I have just recevived this error could not write to hash-join temporary file: No space left on device Can someone please explain how I can stop this occuring. Whereis the hash-join...
1
by: mansa | last post by:
a) A company receives hundreds of orders every week through the mail. Explain carefully how BATCH- PROCESSING might be used. Describe the role of the BATCH TOTAL in this process. Explain how batch...
6
by: Dave Young | last post by:
I'm looking at some code that i've inherited and I'm not really familar with what's going on here and was hoping somone could explain it to me. For reference: f1 is a long f2 is a long ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

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.