5

I am trying to overload the == operator but I am having the following error:

Overloadable unary operator expected

Here is my code

  public bool operator == (MyClass nm1)
  {
       return true;
  }

  public bool operator != (MyClass m2)
  {
       return true;
  }

I followed the msdn note but still getting the same error

2
  • Operator methods are static. That means == and != have 2 operands. Click the 'Operator Overloading Tutorial' link on that same page. Commented Apr 4, 2016 at 19:49
  • public static bool operator == (MyClass m1, MyClass m2) Commented Apr 4, 2016 at 19:50

5 Answers 5

9

When you overload operator == you should do it in a static method that takes two instances as parameters:

public static bool operator == (MyClass leftSide, MyClass rightSide) {
     return true;
}

public static bool operator != (MyClass leftSide, MyClass rightSide) {
     return !(leftSide == rightSide);
}

static context makes the code for your operator feel more "symmetric", in the sense that the code performing the comparison does not belong to a left instance or to the right instance.

In addition, static makes it impossible to "virtualize" the operator (you could still do it inside the implementation by calling a virtual function, but then you have to do it explicitly).

Sign up to request clarification or add additional context in comments.

7 Comments

ohh I see .. will the first parameter be the left operand ?
@Kasparov92 Yes, the first operand is the left side, and the second operand is the right side.
@Kasparov92 Yes, but the order shouldn't matter since equality should be symmetric.
I dont understand this [static context makes the code for your operator feel more "symmetric".]
+ must be static as well, but it allows an unary overload and a binary overload. They are called like this: var resultFromUn = +arg; var resultFromBi = arg0 + arg1;
|
3

You need static, and you need two parameters, not just one.

== and != are always binary operators (two arguments), not unary (one argument).

Comments

2

As you can see in the MSDN tutorial these are binary operators and thus they need two arguments. The operators should also be static as they will be part of the class and not the instance. See this & this.

public static bool operator == (MyClass nm1, MyClass mn2)
{
   return true;
}

public static bool operator != (MyClass m1, MyClass m2)
{
   return true;
}

Comments

2

the correct signature is:

public static bool operator ==(MyClass nm1, MyClass other)
{

}

public static bool operator !=(MyClass nm1, MyClass other)
{

}

The second parameter doesn't necessarily have to be of type MyClass though - but at least one of them must be.

Comments

0
  public static bool operator == (MyClass m1, MyClass m2)
  {
       return true;
  }

  public static bool operator != (MyClass m1, MyClass m2)
  {
       return true;
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.