[ACCEPTED]-C# implicit conversions and == operator-equals-operator

Accepted answer
Score: 18

The implicit operator only works for assignment.

You 8 want to overload the equality (==) operator, as 7 such:

class a
{
    public static bool operator ==(a x, b y)
    {
        return x == y.a;
    }

    public static bool operator !=(a x, b y)
    {
        return !(x == y);
    }
}

class b
{
    public a a{get;set;}
    public static implicit operator a(b b)
    {
        return b.a;
    }
}

This should then allow you to compare 6 two objects of type a and b as suggested in 5 your post.

var x = new a();
var y = new b();
bool c = (x == y); // compiles

Note:

I recommmend simply overriding 4 the GetHashCode and Equals method, as the compiler warns, but 3 as you seem to want to supress them, you 2 can do that as follows.

Change your class 1 declaration of a to:

#pragma warning disable 0660, 0661
class a
#pragma warning restore 0660, 0661
{
    // ...
}
Score: 12

Is it possible to use == operator on different 30 type instances, where one can implicitly 29 convert to another?

Yes.

What did i miss?

Here's 28 the relevant portion of the specification. You 27 missed the highlighted word.

The predefined 26 reference type equality operators require 25 [that] both operands are reference-type 24 values or the literal null. Furthermore, a 23 standard implicit conversion exists from the type 22 of either operand to the type of the other 21 operand.

A user-defined conversion is by 20 definition not a standard conversion. These 19 are reference types. Therefore, the predefined 18 reference type equality operator is not 17 a candidate.

If types must be the same calling 16 ==, then why [double == int] works?

Your 15 supposition that the types must be the same 14 is incorrect. There is a standard implicit 13 conversion from int to double and there 12 is an equality operator that takes two doubles, so 11 this works.

I think you also missed this 10 bit:

It is a compile-time error to use the predefined 9 reference type equality operators to compare 8 two references that are known to be different 7 at compile-time. For example, if the compile-time 6 types of the operands are two class types 5 A and B, and if neither A nor B derives 4 from the other, then it would be impossible 3 for the two operands to reference the 2 same object. Thus, the operation is considered 1 a compile-time error.

Score: 1

I would imagine that you need to actually 8 override the == operator for the types you 7 are interested in. Whether the compile/runtime 6 will still complain even if the types are 5 implicity convertable is something you'll 4 have to experiment with.

public static bool operator ==(a a, b b)
    {
        //Need this check or we can't do obj == null in our Equals implementation
        if (((Object)a) == null)
        {
            return false;
        }
        else
        {
            return a.Equals(b);
        }
    }

Alternatively just 3 use Equals implementations like ole6ka suggests 2 and ensure that the implementation does 1 the type casting you need.

Score: 0

http://msdn.microsoft.com/en-us/library/8edha89s.aspx

In each case, one parameter must be the 2 same type as the class or struct that 1 declares the operator (...)

More Related questions