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.
Please sign in to leave a comment.
How to do that?
Valentin Kipiatkov
CTO and Chief Scientist
JetBrains, Inc
http://www.jetbrains.com
"Develop with pleasure!"
Hello Valentin,
>> In Equals, you have to first ensure that obj is the correct type
>> before casting it.
>>
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.
>> 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.
>>
Or
if (!(obj is ObjType))
return false;
Or
ObjType newObj = obj as ObjType;
if (newObj == null)
return false;
Paul Shealy wrote:
>>> 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.
>>>
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.
>> 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.
>>>>