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

Equals

Wouldn't you agree all of the follwoing should produce the same
result?

r = (o1 == o2);
r = (o2 == o1);
r = object.Equals(o1, o2);
r = object.Equals(o2, o1);
r = (o1.Equals(o2));
r = (o2.Equals(o1));

However if either the objects are null as an exception will be thrown
using one of the last two statements. However if the idea was to make
every object an object why doesn't null implement object? I
understand why this happens as there is no vtable yet why shouldn't
the runtime implement a Equals method for null rather then throwing an
exception? Then every object would truely be an object rather then
every object, providing its is not null, implements Equals.
Is it just me that thinks this way? Does the 2.0 version also have
this problem?

- Kurt
Nov 16 '05 #1
4 2525
Kurt ... As you have discovered, although this call looks object
oriented, it
fails in real world use. It is there for convenience, since coders
expect the old
convention to exist. Equal(x,y) is the practical approach. So Bertrand
Meyers
argues that the convention x.twin should be replaced by clone(x) and
x.is_equal(y) is to be replaced by equal(x,y). OO Software Construction
v2.0
p274-275.

Regards,
Jeff
r = (o1.Equals(o2));
r = (o2.Equals(o1));
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #2
Kurt <ku********@hotmail.com> wrote:
Wouldn't you agree all of the follwoing should produce the same
result?
No.
r = (o1 == o2);
r = (o2 == o1);
These are comparing references, assuming o1 and o2 are declared as
object.
r = object.Equals(o1, o2);
r = object.Equals(o2, o1);
r = (o1.Equals(o2));
r = (o2.Equals(o1));
These will all use the potentially overridden version of Equals.
However if either the objects are null as an exception will be thrown
using one of the last two statements. However if the idea was to make
every object an object why doesn't null implement object?
Because null isn't an object - it's a reference which is to no object.
I understand why this happens as there is no vtable yet why shouldn't
the runtime implement a Equals method for null rather then throwing an
exception?
Because Equals is just like every other method - calling it involves
dereferencing, which you can't do with null.
Then every object would truely be an object rather then
every object, providing its is not null, implements Equals.
Is it just me that thinks this way? Does the 2.0 version also have
this problem?


It's not a problem - it's the way it's meant to be. You shouldn't be
trying to dereference null.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3
Jon Skeet [C# MVP] <sk***@pobox.com> wrote in message news:<MP************************@msnews.microsoft. com>...
Kurt <ku********@hotmail.com> wrote:
Wouldn't you agree all of the follwoing should produce the same
result?
No.

Theorically equals should be a symetrical operation (docs state among
others the following should be true for every implementation of
equals)
x.Equals(a null reference (Nothing)) returns false.
x.Equals(y) returns the same value as y.Equals(x).

Therefore it is not symetrical when y == null

A more practical example:
if hashtable.contains(name) and value is a boolean and value equals
true return true else return false could be written as:
return true.Equals(hashtable[name]);
but cannot be written as:
hastable[name].Equals(true);
You could also use object.Equals(true, hashtable[name]).
But the operator == will cause a compiler warning because object and
true are different types. You can cast true as object but not
hastable[name] to (bool) as you may recieve an invalid cast exception
if value is not typeof boolean.
r = (o1 == o2);
r = (o2 == o1);
These are comparing references, assuming o1 and o2 are declared as
object.
r = object.Equals(o1, o2);
r = object.Equals(o2, o1);
r = (o1.Equals(o2));
r = (o2.Equals(o1));


These will all use the potentially overridden version of Equals.
However if either the objects are null as an exception will be thrown
using one of the last two statements. However if the idea was to make
every object an object why doesn't null implement object?


Because null isn't an object - it's a reference which is to no object.


I was thinking along the lines of the NullObject pattern where there
would be no such thing as no object. "null" should be static instance
such as:
static Object.Null = new NullObject();
I understand why this happens as there is no vtable yet why shouldn't
the runtime implement a Equals method for null rather then throwing an
exception?
Because Equals is just like every other method - calling it involves
dereferencing, which you can't do with null.


See above
Then every object would truely be an object rather then
every object, providing its is not null, implements Equals.
Is it just me that thinks this way? Does the 2.0 version also have
this problem?


It's not a problem - it's the way it's meant to be. You shouldn't be
trying to dereference null.


Okay its not a "problem" but theorically they should be symetrical and
IMHO I shouldn't need to worry about it. I think there is a better
solution. The technical difficulty however is that null can be
assigned to any type. However this didn't stop them from developing
transparent proxies so I don't see why the runtime couldn't generate
an appropriate vtable when one attempts to dereference null as they do
when a transparent proxy is cast to another interface, the generated
vtable would implement object methods and by default throw an
exception for user specific methods.

vtable for normal instance of Foo (created when constructed)
Equals = Object.Equals()
Bar = Foo.Bar()
...

vtable for null instances of Foo (generated when null cast to Foo):
this = Object.Null
Equals = Object.Equals()
Bar = Object.ThrowNullException()
Perhaps taking this further perhaps a special operator could be used
to define the null instance implementation if desired.

class Foo {
public void Bar () { ... }
static Foo _null = new NullFoo();
// special null operator
public static operator null() { return _null; }
class NullFoo { public void Bar() { ... } }
}

now the vtable for null instance could look like this:
this = Foo.Null
Equals = Object.Equals()
Bar = NullFoo.Bar()
...
For interfaces however things become less clear what null
implementation would you like to use? the solution to that would to
use the static method for the desired class.
IFoo f = Foo.Null; // use foo null implemetation - perhaps does
nothing
f = Object.Null; // throws exception when calling Bar

The default implementation of operator null if not explicitly defined
as above would then be public static operator null() { return
Object.Null; }

- Kurt
Nov 16 '05 #4
Kurt <ku********@hotmail.com> wrote:
Jon Skeet [C# MVP] <sk***@pobox.com> wrote:
Kurt <ku********@hotmail.com> wrote:
Wouldn't you agree all of the follwoing should produce the same
result?
No.

Theorically equals should be a symetrical operation (docs state among
others the following should be true for every implementation of
equals)
x.Equals(a null reference (Nothing)) returns false.
x.Equals(y) returns the same value as y.Equals(x).

Therefore it is not symetrical when y == null


You missed part of the documentation:

<quote>
In the list, x, y, and z represent object references that are not a
null reference (Nothing in Visual Basic).
</quote>
A more practical example:
if hashtable.contains(name) and value is a boolean and value equals
true return true else return false could be written as:
return true.Equals(hashtable[name]);
but cannot be written as:
hastable[name].Equals(true);
Can't say I've ever seen that usage pattern, and it seems a very bad
one to me, as it creates loads of boxed booleans. More common is to use
hashtable[name]=name;

and then just use hashtable.ContainsKey(name)
You could also use object.Equals(true, hashtable[name]).
But the operator == will cause a compiler warning because object and
true are different types. You can cast true as object but not
hastable[name] to (bool) as you may recieve an invalid cast exception
if value is not typeof boolean.
So avoid the pattern...
However if either the objects are null as an exception will be thrown
using one of the last two statements. However if the idea was to make
every object an object why doesn't null implement object?


Because null isn't an object - it's a reference which is to no object.


I was thinking along the lines of the NullObject pattern where there
would be no such thing as no object. "null" should be static instance
such as:
static Object.Null = new NullObject();


While that's an appropriate pattern in some situations, I don't
personally think it should be used for *everything*. For a start, it
means you often wouldn't get an error when you're incorrectly trying to
use something which *shouldn't* be null.
I understand why this happens as there is no vtable yet why shouldn't
the runtime implement a Equals method for null rather then throwing an
exception?


Because Equals is just like every other method - calling it involves
dereferencing, which you can't do with null.


See above


I guess you just disagree with the design philosophy of nulls in .NET -
but it's a fairly common philosophy, and one which certainly isn't
going to change.
Then every object would truely be an object rather then
every object, providing its is not null, implements Equals.
Is it just me that thinks this way? Does the 2.0 version also have
this problem?


It's not a problem - it's the way it's meant to be. You shouldn't be
trying to dereference null.


Okay its not a "problem" but theorically they should be symetrical


The docs don't say that.
and IMHO I shouldn't need to worry about it. I think there is a better
solution.
I disagree. I *like* seeing errors when I try to dereference null. I
usually don't want it to just not be equal to things.
The technical difficulty however is that null can be
assigned to any type. However this didn't stop them from developing
transparent proxies so I don't see why the runtime couldn't generate
an appropriate vtable when one attempts to dereference null as they do
when a transparent proxy is cast to another interface, the generated
vtable would implement object methods and by default throw an
exception for user specific methods.


<snip>

There are certainly technical ways of achieving it - but we disagree
about whether or not it would be better than the current system.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #5

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

Similar topics

17
by: Zeng | last post by:
I'm trying to comparing 2 objects (pointer to object) to see if they are the "same" as each other. Here is what the definition of being the "same" object type for both objects, object 1, ...
3
by: Fei Li | last post by:
Hi, take string class as an example, who can explain the difference? Thanks
3
by: Frank Wisniewski | last post by:
Is this suppose to work like this: I have a class called foo in which I tried to override equals and return different answers based on what was passed in. public class foo { private string...
12
by: Rubbrecht Philippe | last post by:
Hi there, According to documentation I read the ArrayList.IndexOf method uses the Object.Equals method to loop through the items in its list and locate the first index of an item that returns...
18
by: JohnR | last post by:
From reading the documentation, this should be a relatively easy thing. I have an arraylist of custom class instances which I want to search with an"indexof" where I'm passing an instance if the...
12
by: cody | last post by:
Why can I overload operator== and operator!= separately having different implementations and additionally I can override equals() also having a different implementation. Why not forbid...
5
by: taumuon | last post by:
I've got an object, Person, that supports IEquatable<Person>. It implements bool Equals(Person obj) as well as overriding bool Equals(object obj) I've got a container type that holds a member...
10
by: r035198x | last post by:
The Object class has five non final methods namely equals, hashCode, toString, clone, and finalize. These were designed to be overridden according to specific general contracts. Other classes that...
7
by: =?Utf-8?B?QWxleCBDb2hu?= | last post by:
In C++, there is an easy technique to provide an overloaded Equals() method. A straightforward translation to C# causes a stack overflow. Why does b.Equals(ba) in the snippet below not understand...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...

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.