How to override the == operator
c#, operator-overloading, operators
Solution
You need to mark the method as `static` and also you have to implement not equal `!=`.
public static bool operator ==(Vector currentVector,Vector anotherVector)
{
return currentVector.CompareTo(anotherVector) == 1 ;
}
You have to implement `==` for two objects.
AND for `!=`
AND
public static bool operator !=(Vector currentVector,Vector anotherVector)
{
return !(currentVector.CompareTo(anotherVector) == 1) ;
}
See: Guidelines for Overloading Equals() and Operator == (C# Programming Guide)
Overloaded operator == implementations should not throw exceptions. Any type that overloads operator == should also overload operator !=.
Problem
How can I define the operator `==` for instances of my class? I tried like this: ``` public bool operator == (Vector anotherVector) { return this.CompareTo(anotherVector) == 1 ; } ``` but it says: overloadable unary operator expected