Azurewrath

Hi all,

I am confused about overriding this method. So my question is, when I implement the == operator for a class, do I have to implement the Equals method for both instance and static methods

I know it caní»t be declared static, but what I mean is this:

Vector3.Equals(object objA, object objB)

objA.Equals(object objB)

Do I have to implement both Because my understanding is, once you implement #1, both methods shows up in intellisense, so #2 is irrelevant. Am I right

What I do is this:

public static bool operator == ( Vector3 v1, Vector3 v2 )

{

return v1.x == v2.x && v1.y == v2.y && v1.z == v2.z;

}

Then:

public override bool Equals ( object obj )

{

Vector3 v = obj as Vector3;

if (v != null )

{

return this == v;

}

return false;

}

Is this the best way to go

Lastly if I doní»t implement the equals method, it still appears, how does it work by default

Thanks a lot,

Aw



Re: Visual C# Language Equals Method

Peter Ritchie

Is Vector3 a class or a struct






Re: Visual C# Language Equals Method

Azurewrath

Oh, it's a class Smile




Thanks alot,
Aw




Re: Visual C# Language Equals Method

Peter Ritchie

If you're type is mutable (e.g. data members can change after construction), I would just overload operator==. If you're planning on supporting adding instances of your class to sorted collections, you'll likely need to overide Equals, GetHashCode, and possibly implement IComparable.

Normally you want to override Equals when you overload operator==; but it's up to how you're expecting it to be used.

With classes the base Equals compares references, not value. With structs the base Equals compares value, so you don't "need" to override Equals; but it's usually faster if you do.






Re: Visual C# Language Equals Method

Azurewrath

Thanks alot Peter. Lastly I wonder if I have to implement both these:

Vector3.Equals(object objA, object objB)

objA.Equals(object objB)


Is it enough to implement #1 and the compiler will do the #2






Thanks,

Aw






Re: Visual C# Language Equals Method

Peter Ritchie

Creating a static Equals method in your class will not get used by any other classes. If you find a method like that useful, go ahead and create it; but because it's static it therefore can't override a virtual method so no other classes can use it.






Re: Visual C# Language Equals Method

Azurewrath

Thanks Peter.

But I didn't understand if I have to implement both To me it seemed like after I wrote #1. #2 worked as expected in the code.




Thanks again,
Aw




Re: Visual C# Language Equals Method

Ji Cheng Wang - MSFT

Hi Azurewrath,

For the two equal methods in a class - Vector3.Equals(object objA, object objB) and objA.Equals(object objB), based on my understanding, it is not necessary to implement both of them. In fact, they are similar to each other. If you implement one of them, you can get the other. However, if you would like to implement both of them in your class, that should be OK.

Hope this helps,

Regards,






Re: Visual C# Language Equals Method

Azurewrath

Thanks man for replying.

That's what I thought! Seems like the compiler is smart enough to provide the other when you already implement one.





Thanks,
Aw




Re: Visual C# Language Equals Method

Azurewrath

Actually you know what, I realized when you override it overrides this:

objA.Equals(object objB)

So this is what you implement. Then other method is done automatically I think.

Because if you write a method that has the same signature as the second one, then it tells you that you hide and and therefore should use the new keyword.

So maybe the underlying implementation for the second method is:

public new bool Equals ( object objA, object objB )

{

return true;

}




What do you guys think





Thanks,
Aw




Re: Visual C# Language Equals Method

Peter Ritchie

Ji Cheng Wang - MSFT wrote:

Hi Azurewrath,

For the two equal methods in a class - Vector3.Equals(object objA, object objB) and objA.Equals(object objB), based on my understanding, it is not necessary to implement both of them. In fact, they are similar to each other. If you implement one of them, you can get the other. However, if you would like to implement both of them in your class, that should be OK.

Hope this helps,

Regards,

Actually, you don't get one with the other. If you don't implement Equals(object objectA, object objectB) there's no way to compare two null Vector3 objects without an implicit cast, if you don't want null==null to be true for those classes.




Re: Visual C# Language Equals Method

Peter Ritchie

Another thing to consider is that not all code can access your operator==. Generic code, for example, cannot access operator== because operator== is not a member of any interfaces. In order to test for equality you must use the Equals method. For example:

Code Block

Method(T one, T two)

{

if(one == two) // compile error

{

}

}

So, you must implement something like:

Code Block

Method(T one, T two)

{

if(one != null && one.Equals(two))

{

}

}






Re: Visual C# Language Equals Method

Azurewrath

Thanks Peter.

But this is the only one overridable right :

Vector3.Equals(object objA, object objB)

The other one can only be hidden, right




Thanks again,
Aw




Re: Visual C# Language Equals Method

Peter Ritchie

If you're not deriving from another class or you're deriving from Object, the only overridable members are Equals(Object obj), GetHashCode() and ToString().

If you're talking about static Boolean Object.Equals(object objA, object objB), that's static and can't be "overridden".

If you override Equals(Object obj), Object.Equals(Object objA, object objB) will call your class's Equals(Object obj) when both objects aren't null... But, if you want something other than true when your two object are null or avoid boxing if your type is a value type, you'll have to create another method and use that. For types that act like values, "equal" doesn't make sense when both objects are null.






Re: Visual C# Language Equals Method

Azurewrath

1. Equals(Object obj)
2. Object.Equals(Object objA, object objB)

Thanks Peter. I meant to say 1, sorry got mixed them up.

Now I have another question though.

If 2 calls one, then what happens if the objA is not a vector3 object And what if obj is a null object

Should I accomodate null objects in my overriding Equals method




Thanks,
Aw