generated Equals

Just used the Generate Equals and GetHashCode feature for the first time
today... very cool! But one problem: the code for Equals doesn't allow symmetry
with derived types. For an implementation of bool Equals (obj) in a class
A, the book Effective C# mentions the problem of having a class B derived
from A. Then, if we have variables a and b, of types A and B, a.Equals(b)
could be true while b.Equals(a) is false. In Equals, you have to first ensure
that obj is the correct type before casting it.


4 comments
Comment actions Permalink

In Equals, you have to first ensure that obj is the correct
type before casting it.


How to do that?

Valentin Kipiatkov
CTO and Chief Scientist
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

Just used the Generate Equals and GetHashCode feature for the first
time today... very cool! But one problem: the code for Equals doesn't
allow symmetry with derived types. For an implementation of bool
Equals (obj) in a class A, the book Effective C# mentions the problem
of having a class B derived from A. Then, if we have variables a and
b, of types A and B, a.Equals(b) could be true while b.Equals(a) is
false. In Equals, you have to first ensure that obj is the correct
type before casting it.



0
Comment actions Permalink

Hello Valentin,

>> In Equals, you have to first ensure that obj is the correct type
>> before casting it.
>>

How to do that?


Do if (GetType () != obj.GetType ()) return false; after checking obj for
null. See http://weblogs.asp.net/tgraham/archive/2004/03/23/94870.aspx for
a sample.


Valentin Kipiatkov
CTO and Chief Scientist
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"

>> Just used the Generate Equals and GetHashCode feature for the first
>> time today... very cool! But one problem: the code for Equals doesn't
>> allow symmetry with derived types. For an implementation of bool
>> Equals (obj) in a class A, the book Effective C# mentions the problem
>> of having a class B derived from A. Then, if we have variables a and
>> b, of types A and B, a.Equals(b) could be true while b.Equals(a) is
>> false. In Equals, you have to first ensure that obj is the correct
>> type before casting it.
>>


0
Comment actions Permalink

Or

if (!(obj is ObjType))
return false;

Or

ObjType newObj = obj as ObjType;
if (newObj == null)
return false;

Paul Shealy wrote:

Hello Valentin,

>>> In Equals, you have to first ensure that obj is the correct type
>>> before casting it.
>>>
>> How to do that?


Do if (GetType () != obj.GetType ()) return false; after checking obj
for null. See
http://weblogs.asp.net/tgraham/archive/2004/03/23/94870.aspx for a sample.

>>
>> Valentin Kipiatkov
>> CTO and Chief Scientist
>> JetBrains, Inc
>> http://www.jetbrains.com
>> "Develop with pleasure!"
>>> Just used the Generate Equals and GetHashCode feature for the first
>>> time today... very cool! But one problem: the code for Equals doesn't
>>> allow symmetry with derived types. For an implementation of bool
>>> Equals (obj) in a class A, the book Effective C# mentions the problem
>>> of having a class B derived from A. Then, if we have variables a and
>>> b, of types A and B, a.Equals(b) could be true while b.Equals(a) is
>>> false. In Equals, you have to first ensure that obj is the correct
>>> type before casting it.
>>>


0
Comment actions Permalink

Or

if (!(obj is ObjType))
return false;
Or

ObjType newObj = obj as ObjType;
if (newObj == null)
return false;


No.... that's the point. If you have a base class A and a derived class B,
then in A the casts will always succeed, but in B the cast will fail if obj
is of type A. Then, you've broken symmetry, so a.Equals(b) can be true while
b.Equals(a) is false.

Paul Shealy wrote:

>> Hello Valentin,
>>
>>>> In Equals, you have to first ensure that obj is the correct type
>>>> before casting it.
>>>>
>>> How to do that?
>>>
>> Do if (GetType () != obj.GetType ()) return false; after checking
>> obj for null. See
>> http://weblogs.asp.net/tgraham/archive/2004/03/23/94870.aspx for a
>> sample.
>>
>>> Valentin Kipiatkov
>>> CTO and Chief Scientist
>>> JetBrains, Inc
>>> http://www.jetbrains.com
>>> "Develop with pleasure!"
>>>> Just used the Generate Equals and GetHashCode feature for the first
>>>> time today... very cool! But one problem: the code for Equals
>>>> doesn't allow symmetry with derived types. For an implementation of
>>>> bool Equals (obj) in a class A, the book Effective C# mentions the
>>>> problem of having a class B derived from A. Then, if we have
>>>> variables a and b, of types A and B, a.Equals(b) could be true
>>>> while b.Equals(a) is false. In Equals, you have to first ensure
>>>> that obj is the correct type before casting it.
>>>>


0

Please sign in to leave a comment.