[Mono-list] Enum.Equals() confusion
Dietmar Maurer
dietmar@ximian.com
18 Mar 2002 10:23:08 +0100
On Sun, 2002-03-17 at 23:34, Nick Drochak wrote:
> | > enum TestingEnum {This, Is, A, Test};
> | > enum TestingEnum2 {This, Is, A, Test};
> | > ...
> | > Enum e1 = new TestingEnum();
> | > Enum e2 = new TestingEnum();
> | > Enum e3 = new TestingEnum2();
> | >
> | > bool b1 = e1.Equals(e2);
> | > bool b2 = e1.Equals(e3);
> | > bool b3 = TestingEnum.Test.Equals(TestingEnum2.Test);
>
> | The correct answer is:
> | b1 = True
> | b2 = False
> | b3 = False
> |
> | The Equals() method determines whether the objects are
> | equivalent not whether they are the same object. Thus B1 is
> | true. B2 is false because they operands are obviously not
> | even of the same type. And B3 is false because the operands
> | are again not the same.
>
> Thank you. That answers part of it.
>
> But, I'm still a little confused about how to implement Enum.Equals().
> Is the same Equals method called in the case of b1 and b3? If so, how
> does it know?
>
I have fixed the implementation - we simply need to check if objects are
of the same type:
public override bool Equals (object obj)
{
if (obj.GetType() != this.GetType())
return false;
object v1 = this.get_value ();
object v2 = ((Enum)obj).get_value ();
return v1.Equals (v2);
}