Why explicit conversion from SomeType* to IntPtr is not ambiguous (according
to standart)?
Example:
// System.IntPtr
class IntPtr
{
public static explicit System.IntPtr (int);
public static explicit System.IntPtr (long);
public static explicit System.IntPtr (void*);
}
struct SomeType {}
unsafe void f ( SomeType *p )
{
if ( (IntPtr)p == IntPtr.Zero ) // <-
{
// ...
}
}
Above example contain explicit conversion from SomeType* to IntPtr. The
question is how C# compiler select explicit conversion. Standart say: "A
user-defined explicit conversion consists of an optional standard explicit
conversion, followed by execution of a user-defined implicit or explicit
conversion operator, followed by another optional standard explicit
conversion." There are 3 user-defined exlicit conversions. Conversion
System.IntPtr (int) is removing from list because long is more encompassing
type than int (13.4.2). But neither of two remaining types is more
encompassing than other.
SomeType *p -> long -> IntPtr (explicit+user-defined explicit conversion)
SomeType *p -> void* -> IntPtr (implicit+user-defined explicit conversion)
If neither of two conversions is better than conversion is ambiguos. But
compiler think that conversion with through void* is better. Why? I not
found where standart say that implicit conversion is better than explicit
conversion.
Questions:
1. Is conversion from void* to IntPtr is processed as user-defined?
2. Why conversion sequence (System.IntPtr)(void*(p)) is better than
(System.IntPtr)(long(p))?
Any suggestions.
Alex.