What's the difference, here?

  • Thread starter Thread starter Jon Shemitz
  • Start date Start date
J

Jon Shemitz

Why is the cast in Child.Test valid and the cast in Base.Test is not?
"C# Language Specification" section 6.4.4 makes me think that perhaps
the Base.Test cast is ambiguous in some way, but I don't see it. I'd
expect an implicit conversion of Value to long, Derived.Derived() to
turn that into a Derived, then an implicit child-to-parent conversion
of that to Base.

What am I missing?

class Parent
{
public static explicit operator float (Parent Instance)
{
return 1f; // just to return something
}
}

class Child: Parent
{
static void Test()
{
Child Instance = new Child();
double D = (double) Instance; // OK
}
}

class Base
{
static void Test()
{
int Value = 1;
Base Instance = (Base) Value; // Not
}
}

class Derived: Base
{
public static explicit operator Derived(long Value)
{
return new Derived();
}
}
 
Mattias Sjögren said:
That's an explicit conversion so you have to make it

Base Instance = (Derived) Value;

Thanks - but how is that different from the "(double) new Child()"
which DOES work, even though the explicit conversion there is to a
float?
 
Back
Top