object.equals
-
Hi, I have two objects, one of type A and one of type B. I want to be able to compare these objects. In my code I want to test for equlity with A.Equals and B.Equals. But I also want to be able to compare A and B type objects with other things. So my code looks like this: class A { override bool Equals(object x) { if (x is B) { //do something } if (x is ...) { //do something } ... } } and the some thing for class B. Is there a better (more elegant) way? Thanks a lot, Andrei andreimatei@home.ro
-
Hi, I have two objects, one of type A and one of type B. I want to be able to compare these objects. In my code I want to test for equlity with A.Equals and B.Equals. But I also want to be able to compare A and B type objects with other things. So my code looks like this: class A { override bool Equals(object x) { if (x is B) { //do something } if (x is ...) { //do something } ... } } and the some thing for class B. Is there a better (more elegant) way? Thanks a lot, Andrei andreimatei@home.ro
Andrei Matei wrote: Is there a better (more elegant) way? Not really, but if it gets more complex, have a look at implementing the IComparer interface. This will allow sorting and other goodies too. Also remember
Equals(object)
does not makeif (a == b)
true as well. You will need to override the operator for that, or call a.Equals(b). Cheers :)