relational operator parameters

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?
 
M

Mattias Sjögren

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
 
S

Steve

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top