Home > Backend Development > C++ > Why Does C# Throw an 'Ambiguous Invocation' Error When Using Method Groups but Not Anonymous Methods?

Why Does C# Throw an 'Ambiguous Invocation' Error When Using Method Groups but Not Anonymous Methods?

Susan Sarandon
Release: 2025-01-13 06:08:12
Original
448 people have browsed it

Why Does C# Throw an

C# compiler ambiguous call error: anonymous method and method group

C# provides two syntaxes for calling functions: anonymous methods (or lambda syntax) and method groups. Ambiguities may arise using method group syntax when a function has multiple overloads that accept different delegate types.

Consider the following code example:

<code class="language-csharp">class Program
{
    public static void Main()
    {
        // 匿名方法(编译成功)
        Method(() => classWithSimpleMethods.GetString());
        Method(() => classWithSimpleMethods.DoNothing());

        // 使用显式转换的方法组(也编译成功)
        Method((Func<string>)classWithSimpleMethods.GetString);
        Method((Action)classWithSimpleMethods.DoNothing);

        // 方法组(错误:“歧义调用”)
        Method(classWithSimpleMethods.GetString);
        Method(classWithSimpleMethods.DoNothing);
    }

    public static void Method(Func<string> func) { /* 执行某些操作 */ }
    public static void Method(Action action) { /* 执行某些操作 */ }
}</code>
Copy after login

In this example, the Method function has two overloads, one that accepts Action and the other that accepts Func<string>. When the function is called using an anonymous method or an explicit cast to the correct delegate type, the code compiles without errors. However, when using method group syntax, the compiler reports an "ambiguous call" error.

Understanding ambiguity

The C# language specification states that there are implicit conversions from method groups to compatible delegate types. However, in this case, the compiler faces ambiguity because method group classWithSimpleMethods.GetString can be implicitly converted to Func<string> and Action.

According to the specification, overload resolution involves selecting the most applicable candidate based on matching argument types. Although the parameter list of the GetString method is compatible with both overloads, the compiler cannot determine the best match because it does not consider the return type during overload resolution.

Resolving ambiguities

To resolve ambiguities, you can use an explicit cast to the correct delegate type, as shown in the code example. However, this approach can be tedious and error-prone.

Fortunately, in C# 7.3 and later, the compiler has improved to handle this situation. Overload candidates are now ranked based on compatibility with the target delegate type, eliminating the need for explicit conversions in this case.

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template