KnowledgeHub
Questions
Tags
Users
Search
Alex Rivera
|
Logout
Edit Question
Title
Body
I came across this recently, up until now I have been happily overriding the equality operator ( == ) and/or Equals method in order to see if two references types actually contained the same data (i.e. two different instances that look the same). I have been using this even more since I have been getting more in to automated testing (comparing reference/expected data against that returned). While looking over some of the coding standards guidelines in MSDN I came across an article that advises against it. Now I understand why the article is saying this (because they are not the same instance ) but it does not answer the question: What is the best way to compare two reference types? Should we implement IComparable ? (I have also seen mention that this should be reserved for value types only). Is there some interface I don't know about? Should we just roll our own?! Many Thanks ^_^ Update Looks like I had mis-read some of the documentation (it's been a long day) and overriding Equals may be the way to go.. If you are implementing reference types, you should consider overriding the Equals method on a reference type if your type looks like a base type such as a Point, String, BigNumber, and so on. Most reference types should not overload the equality operator, even if they override Equals . However, if you are implementing a reference type that is intended to have value semantics, such as
Tags (comma-separated)
Save Edits
Cancel