Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous C# method call with delegates [duplicate]

In my application, I have code similar to following:

class Program
    {
        static void Main(string[] args)
        {
            Method(uri => Task.FromResult(uri));
        }

        static void Method(Func<Uri, Uri> transformer)
        {
            throw new NotImplementedException();
        }

        static void Method(Func<Uri, Task<Uri>> transformer)
        {
            throw new NotImplementedException();
        }
    }

As expected, running this code calls the second overload of 'Method', the one expecting a function delegate that returns a task. However, if i change the code to avoid using the anonymous method in Main:

class Program
    {
        static void Main(string[] args)
        {
            Method(Method2);
        }

        static Task<Uri> Method2(Uri uri)
        {
            return Task.FromResult(uri);
        }

        static void Method(Func<Uri, Uri> transformer)
        {
            throw new NotImplementedException();
        }

        static void Method(Func<Uri, Task<Uri>> transformer)
        {
            throw new NotImplementedException();
        }
    }

The C# compiler now complains that my call to 'Method' is ambiguous. What am i missing?

like image 977
richzilla Avatar asked Aug 27 '15 08:08

richzilla


People also ask

What is ambiguous base class in C?

Ambiguous base classes (C++ only) When you derive classes, ambiguities can result if base and derived classes have members with the same names. Access to a base class member is ambiguous if you use a name or qualified name that does not refer to a unique function or object.

What are ambiguity in derived classes?

When you derive classes, ambiguities can result if base and derived classes have members with the same names. Access to a base class member is ambiguous if you use a name or qualified name that does not refer to a unique function or object. The declaration of a member with an ambiguous name in a derived class is not an error.

What is the ambiguous case in geometry?

For those of you who need a reminder, the ambiguous case occurs when one uses the law of sines to determine missing measures of a triangle when given two sides and an angle opposite one of those angles (SSA). ... If angle A is acute, and a = h, one possible triangle exists

What is ambiguous a CFG?

A CFG is said to ambiguous if there exists more than one derivation tree for the given input string i.e., more than one LeftMost Derivation Tree (LMDT) or RightMost Derivation Tree (RMDT). Definition: G = (V,T,P,S) is a CFG is said to be ambiguous if and only if there exist a string in T* that has more than on parse tree.


1 Answers

The long answer is at https://stackoverflow.com/a/2058854/1223597 (as richzilla pointed out).

The short answer is that the C# compiler team chose to make method group conversions (like Method(Method2)) ignore the return type (here of Method2). This gives them flexibility in how Expression trees are parsed. Unfortunately that means the compiler cannot implicitly choose between your 2 Method signatures.

When doing a lambda conversion, (Method(uri => Task.FromResult(uri))), the compiler team doesn't need to worry about expression tree parsing, so they do consider return types.

like image 50
Joel V. Earnest-DeYoung Avatar answered Nov 08 '22 08:11

Joel V. Earnest-DeYoung