Why I cannot define both implicit and explicit operators like so?
public class C
{
public static implicit operator string(C c)
{
return "implicit";
}
public static explicit operator string(C c)
{
return "explicit";
}
}
You can do this hack though :)
class Program
{
public class A
{
}
public class B
{
public static implicit operator A(B b)
{
Console.WriteLine("implicit");
return new A();
}
}
public class C : B
{
public static explicit operator A(C c)
{
Console.WriteLine("explicit");
return new A();
}
}
static void Main(string[] args)
{
C c = new C();
A a = c;
A b = (A) c;
}
}
This prints:
implicit
explicit