3

I have these Methods:

    static void OverloadedMethod(Action<ulong> handlerAction)
    {            
    }

    static void OverloadedMethod(Action<float> handlerAction)
    {
    }

    static void HandlerA(ulong dataProgress)
    {
    }

    static void HandlerB(float dataProgress)
    {
    }

I can call

OverloadedMethod(HandlerA);

without problems, but if i try to call

OverloadedMethod(HandlerB);

Compiler complains: Ambiguous Invocation.

I have read this article but i dont understand why the compiler knows which method should it choose if the parameter is a ulong but it cannot resolve the same situation if the parameter is a float...

Nomada
  • 343
  • 2
  • 10

2 Answers2

1

According to MSDN, there is a pre-defined implicit conversion from ulong to float. As a result, the implicit conversion prevents the compiler from identifying which method to call.

David L
  • 32,885
  • 8
  • 62
  • 93
  • 2
    But if we have methods like `static void SomeMethod(float a){Console.WriteLine("In float method");}` and `static void SomeMethod(ulong a) { Console.WriteLine("In ulong method");}` and call it like `SomeMethod(10);` then it correctly resolves and there is no ambiguous error from compiler. So not really sure if it is implicit conversion preventing overload resolution. – Habib Nov 10 '14 at 17:19
  • This is standard type conversion, we are talking generic types here, there is no implicit casting for generics. – Guillermo Ruffino Nov 10 '14 at 17:23
  • @BartoszKP, after reading your answer: so, as a float can be a float or a float implicitly converted from ulong _implicit conversion occurs and neither of them is better_. But as a ulong cannot be implicitly obtained from a float there is no doubt, right ? – Nomada Nov 10 '14 at 18:46
-2

Take a look at covariance and contravariance here

Guillermo Ruffino
  • 2,940
  • 1
  • 26
  • 23
  • 1
    Link-only answers are not generally acceptable. Include the link in your answer, but you need to summarize the relevant bits directly in your post here. – tnw Nov 10 '14 at 17:24