figured it out. I have to use Object.Equals. For reference types it will compare references. for value types it will compare the values. the "=" sign won't work, that just compares the references.
Main Topics
Browse All TopicsFolks,
I'm looking for a way to compare two objects. If the objects are of different types, then then th comparison should return false (they are no equal). If the objects are the same type, then the method should use that type's default equality comparer and return the result. So, for example, if the two objects are doubles, then the doubles can be compared. I don't know ahead of time what the object types are.
Thanks!
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
Note that Equals does not check for two object to be the same when they are of the same type, unless they also refer to the same object in memory. This doesn't happen often, normally you want to know equality of the fields of the type.
The first answer you received is comparing the types as you said in the beginning of your question (if the objects are of different types, then comparison should return false).
Anyway, I hopped on this train after your request for closing, so I won't object. I just feel that your current Object.Equals is too light for your requirement and that you should add the first comment to your method and add a field-comparison to that (not provided here yet).
-- Abel --
Hi Abel,
thanks so much for jumping in here. I absolutely want a complete solution. Are you sure about this behavior for Object.Equals? I just ran a test where the "=" failed for two doubles that weren't strongly typed, but held the same value. Object.Equals compared just fine.
I got this from the MSDN: The default implementation of Equals supports reference equality for reference types, and bitwise equality for value types. Reference equality means the object references that are compared refer to the same object. Bitwise equality means the objects that are compared have the same binary representation.
does this explain the behavior I saw?
Thanks :)
The point that I wanted to make is that Equals may not always yield the result you expect (or perhaps it is what you expect, but let me explain anyway). The problem is with boxing and with strings. When boxing is in place, Equals returns true, but "==" returns false depending on the situation. When you use objects, two objects that are of the same type, can still yield false with Equals and with strings, the same content will always yield true. Example:
-- Abel --
PS: comparing the string value of the type is never a good thing. If you want type equality (I know, you don't, but if), just compare the two types: the type (from GetType or typeof(..)) is a shared static object, which will return true always, both with "==" and "Equals", if both are of the same type.
Hi Abel,
Thanks so much for the explanation. Its sounds like Equals really is the best way to compare two objects?
For reference types, if an object doesn't overload Equals then two objects are equal if they point to the same object. All common value types override Equals (I believe...correct?) so two objects will be equals if their values are equal.
Yes, with the exception of strings, which are objects, no value types, and are considered equal when the contents is equal. And of course with the exception of any class where Equals is overloaded to give content (field) or other type of equality.
For value types: yes, you are correct, Equals is always overloaded. The "==" gives different results (value equality) then "==" (reference equality) for boxed value types, which seems odd and is sometimes counter intuitive.
Business Accounts
Answer for Membership
by: p_davisPosted on 2009-08-17 at 10:45:07ID: 25116583
if(firstObject.GetType() == secondObject.GetType())
//do your thing.