How does the C# compiler handle overloading explicit cast operators?

Viewed 259

The compiler should translate this code:

public static explicit operator Int64(MyNumber n)
{
    return n.ToInteger();
}

public static explicit operator Double(MyNumber n)
{
    return n.ToDouble();
}

to two methods that have the same name and signature but differ only by their return type, e.g.

public static Int64 ExplicitCast(MyNumber n)
...

public static Double ExplicitCast(MyNumber n)
...

However, we are not allowed to have methods that differ only by their return type. What happens behind the curtains?

2 Answers
Related