What's the difference, here?

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();
}
}
 
M

Mattias Sjögren

Jon,
Derived.Derived() to turn that into a Derived

That's an explicit conversion so you have to make it

Base Instance = (Derived) Value;



Mattias
 
J

Jon Shemitz

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top