relational operator parameters

  • Thread starter Thread starter Steve
  • Start date Start date
S

Steve

I'm confused with creating the == operator for my class. It appears that c#
requires the signature:
public static bool operator==(Type a, Type b);

I want to use:
public bool operator==(Type RightHandSide)
{
return this.SomeMember = RightHandSide.SomeMember;
}


This can't be correct, that I can't make a member operator to my class and
use the 'this' reference?
 
This can't be correct, that I can't make a member operator to my class and
use the 'this' reference?

The operator should work even if the left hand side operand is null.

Type a = null, b = null;
if (a == b) ...

If it was an instance operator this would throw a
NullReferenceException.


Mattias
 
Mattias Sjögren said:
The operator should work even if the left hand side operand is null.

Type a = null, b = null;
if (a == b) ...

If it was an instance operator this would throw a
NullReferenceException.


Ah. I see. I have since added the Equals() method
thanks for the heads up!
 
Back
Top