D
dave_henderson1
I currently have the following code:
struct DoubleWrapper
{
public DoubleWrapper(double doubleValue) {}
public static implicit operator DoubleWrapper(double doubleValue)
{ return new DoubleWrapper(doubleValue); }
}
struct IntegerWrapper
{
public IntegerWrapper(int intValue) {}
public static implicit operator IntegerWrapper(int intValue)
{ return new IntegerWrapper(intValue); }
}
class MyClass
{
public void MyMethod(DoubleWrapper doubleValue) {}
public void MyMethod(IntegerWrapper intValue) {}
static void Main()
{
MyClass myClass = new MyClass();
myClass.MyMethod(1);
}
}
The code doesn't compile, outputting the following error:
error CS0121: The call is ambiguous between the following methods or
properties:
'ConsoleApplication1.MyClass.MyMethod(ConsoleApplication1.IntegerWrapper)'
and
'ConsoleApplication1.MyClass.MyMethod(ConsoleApplication1.DoubleWrapper)'
I thought it might be because the compiler couldn't tell if 1 was an
int or a double, but if i replace the two MyMethod() methods by
changing the types as follows, it works:
public void MyMethod(double doubleValue) {}
public void MyMethod(int intValue) {}
Can someone explain why my code produces that error?
Thanks
struct DoubleWrapper
{
public DoubleWrapper(double doubleValue) {}
public static implicit operator DoubleWrapper(double doubleValue)
{ return new DoubleWrapper(doubleValue); }
}
struct IntegerWrapper
{
public IntegerWrapper(int intValue) {}
public static implicit operator IntegerWrapper(int intValue)
{ return new IntegerWrapper(intValue); }
}
class MyClass
{
public void MyMethod(DoubleWrapper doubleValue) {}
public void MyMethod(IntegerWrapper intValue) {}
static void Main()
{
MyClass myClass = new MyClass();
myClass.MyMethod(1);
}
}
The code doesn't compile, outputting the following error:
error CS0121: The call is ambiguous between the following methods or
properties:
'ConsoleApplication1.MyClass.MyMethod(ConsoleApplication1.IntegerWrapper)'
and
'ConsoleApplication1.MyClass.MyMethod(ConsoleApplication1.DoubleWrapper)'
I thought it might be because the compiler couldn't tell if 1 was an
int or a double, but if i replace the two MyMethod() methods by
changing the types as follows, it works:
public void MyMethod(double doubleValue) {}
public void MyMethod(int intValue) {}
Can someone explain why my code produces that error?
Thanks