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 / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

Define operator == and !=

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tony Johansson - 09 Mar 2008 13:53 GMT
Hello!

If I define operator == and != is then nessesary to override method Equals
and GetHashCode that is inherited from
System.Object ?

If yes will Equals have the same functionalitty as operator == ?

So can I then just restrict the functionality to one method and just call
this method from the other ?

Does this sound correct ?

//Tony
Jon Skeet [C# MVP] - 09 Mar 2008 14:51 GMT
> If I define operator == and != is then nessesary to override method Equals
> and GetHashCode that is inherited from
> System.Object ?

It's at least a very good idea to do so.

> If yes will Equals have the same functionalitty as operator == ?

Again, it's a very good idea to make Equals and == do the same thing.

> So can I then just restrict the functionality to one method and just call
> this method from the other ?
>
> Does this sound correct ?

Yes.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk

Phil - 09 Mar 2008 20:47 GMT
> Hello!
>
[quoted text clipped - 10 lines]
>
> //Tony

They are not strictly identical, as operator== might be called with two null
references, whereas this cannot arise when you are overriding object's
virtual Equals method.

If using VS2005, overriding GetHashCode is very simple - when you type
"public override" the intellisense suggests things you can override - if you
click GetHashCode it writes the whole function for you, calling
"base.GetHashCode()". You only need to rewrite the code in it if your
redefinition of equality causes two objects that are "equal" give different
hash codes.
Jon Skeet [C# MVP] - 09 Mar 2008 23:21 GMT
<snip>

> If using VS2005, overriding GetHashCode is very simple - when you type
> "public override" the intellisense suggests things you can override - if you
> click GetHashCode it writes the whole function for you, calling
> "base.GetHashCode()". You only need to rewrite the code in it if your
> redefinition of equality causes two objects that are "equal" give different
> hash codes.

On the other hand, that's true of all of ==, !=, Equals and
GetHashCode.

If the default implementation isn't appropriate for one of them,
chances are it won't be appropriate for any of them - at least for
reference types.

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
World class .NET training in the UK: http://iterativetraining.co.uk


Rate this thread:







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.