Some code for context:
class a
{
}
class b
{
public a a{get;set;}
public static implicit operator a(b b)
{
return b.a;
}
}
a a=null;
b b=null;
a = b;
//compiler: cannot apply operator '==' to operands of type tralala...
bool c = a == b;
Is it possible to use == operator on different type instances, where one can implicitly convert to another? What did i miss?
Edit:
If types must be the same calling ==, then why
int a=1;
double b=1;
bool c=a==b;
works?
The
implicitoperator only works for assignment.You want to overload the equality (
==) operator, as such:This should then allow you to compare two objects of type
aandbas suggested in your post.Note:
I recommmend simply overriding the
GetHashCodeandEqualsmethod, as the compiler warns, but as you seem to want to supress them, you can do that as follows.Change your class declaration of
ato: