.net object equality
.net, c#
Solution
I would suggest you use
object.Equals(o1, o2)
as that will cope with nullity as well. (That assumes you want two null references to compare as equal.)
You should not use `==` because operators are not applied polymorphically; the types overload == but they don't override it (there's nothing to override). If you use
o1 == o2
that will compare them for reference identity, because the variables are declared to be of type `object`.
Using `o1.Equals(o2)` will work except in the case where `o1` is null - at which point it would throw a `NullReferenceException`.
Problem
Lets say we have two objects o1 & o2 defined as System.Object, in my situtaion o1 & o2 can be any of the following types: - String - Int32 - Double - Boolean - DateTime - DBNull So how can I check that o1 & o2 are equal, therefore are the same object or both have the same type & value. Can I just do `o1 == o2` or do I need to do `o1.Equals(o2)` or something else? Thanks, AJ