Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / .NET SDK / December 2003

Tip: Looking for answers? Try searching our database.

Implementing GetHashCode and Equals methods

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Edward Diener - 28 Dec 2003 15:21 GMT
I want to provide GetHashCode and Equals methods for my components. My
understanding of these methods is that if two of the same type of components
return the same value from GetHashCode, then they must return true from the
Equals method if compared.

In the GetHasCode documentation it is very clearly stated that "The hash
function must return exactly the same value regardless of any changes that
are made to the object." Clearly the Equals method, if it is not dependent
on the object's actual reference value of a reference type but on property
or field values of the object instead, is dependent on changes to the
object. Therefore I do not understand how the Equals value, which depends on
changes to the object, can be in sync with the GetHashCode value, which
depends on the immutability of some value. Can anyone enlighten me on these
subjects ?
Mattias Sj?gren - 29 Dec 2003 20:03 GMT
Edward,

>My
>understanding of these methods is that if two of the same type of components
>return the same value from GetHashCode, then they must return true from the
>Equals method if compared.

No, two objects can return the same hash code without being considered
equal. However, the reverse is true. If one.Equals(another) == true
then they should return the same hash code.

Mattias

Signature

Mattias Sjögren [MVP]  mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.