Home >Backend Development >C++ >Why Does C# Throw an 'Ambiguous Invocation' Error When Using Method Groups with Overloaded Delegate Methods?

Why Does C# Throw an 'Ambiguous Invocation' Error When Using Method Groups with Overloaded Delegate Methods?

Patricia Arquette
Patricia ArquetteOriginal
2025-01-13 10:35:43468browse

Why Does C# Throw an

C# compiler ambiguous call error: method group and delegate type

In C#, when calling a function with multiple overloads using method group syntax, the compiler may encounter an "ambiguous call" error. This error occurs because the compiler cannot determine which overload to call when there are two method groups with compatible delegate types.

Consider the following code example:

<code class="language-csharp">public class ClassWithDelegateMethods
{
    public void Method(Func<string> func) { /* do something */ }
    public void Method(Action action) { /* do something */ }
}

public class ClassWithSimpleMethods
{
    public string GetString() { return ""; }
    public void DoNothing() { }
}

public class Program
{
    public static void Main(string[] args)
    {
        // 错误:歧义调用
        ClassWithDelegateMethods classWithDelegateMethods = new ClassWithDelegateMethods();
        classWithDelegateMethods.Method(classWithSimpleMethods.GetString); // 这里会报错
        classWithDelegateMethods.Method(classWithSimpleMethods.DoNothing); // 这里会报错
    }
}</code>
Both overloads of

ClassWithDelegateMethods.Method can be called using anonymous methods or lambda expressions without any problems. However, calling classWithSimpleMethods.GetString or classWithSimpleMethods.DoNothing using method group syntax results in an ambiguity error.

The reason for this error lies in the implicit conversion rules between method groups and delegate types. According to the C# specification, there are implicit conversions between method groups to compatible delegate types. However, for method groups with overloads, the specification does not define a mechanism for determining which overload to convert to.

Avoid ambiguous errors

To resolve ambiguity errors, method groups can be explicitly cast to the required delegate type. For example:

<code class="language-csharp">// 无错误
classWithDelegateMethods.Method((Func<string>)classWithSimpleMethods.GetString);
classWithDelegateMethods.Method((Action)classWithSimpleMethods.DoNothing);</code>

Alternatively, starting with C# 7.3, the compiler's improved overload candidate selection algorithm eliminates this problem. Therefore, in C# 7.3 and above, the above code example should compile without explicit conversion.

The above is the detailed content of Why Does C# Throw an 'Ambiguous Invocation' Error When Using Method Groups with Overloaded Delegate Methods?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn