Overload operator == for null

I have a class with a name Messagethat overloads these statements:

public static bool operator ==(Message left, Message right)
public static bool operator !=(Message left, Message right)

public static bool operator ==(Message left, string right)
public static bool operator !=(Message left, string right)

public static bool operator ==(string left, Message right)
public static bool operator !=(string left, Message right)

I want to make statements ==and !=continued to compare reference types other than Stringand Message, but

var message = new Message();
var isNull = message == null;

gives me the following:

The call is ambiguous between the following methods or properties: 'Message.operator == (Message, Message)' and 'Message.operator == (Message, string)'

I know this because both Messageand Stringare reference types, and both of them may be null, but I want to be able to use ==opreator to check whether a zero message.

== ? object object.ReferenceEquals(object, object) , .

+5
1

operator ==(Message left, object right) right, , , .

Message, . . .

+6

All Articles