Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous call between (Func<IInterface>) and (Func<Task<IInterface>>)

Tags:

c#

Why do I get ambiguous call error between (Func<IInterface>) and (Func<Task<IInterface>>) for the next code sample? And how can I avoid this error without replacing method group call?

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

    public static IInterface GetObject() => null;

    public static void Method(Func<IInterface> func) => 
        Console.WriteLine("First");

    public static void Method(Func<Task<IInterface>> funcAsync) => 
        Console.WriteLine("Second");
 }

 public interface IInterface { }
like image 209
Roberto Deresu Avatar asked Jan 25 '26 15:01

Roberto Deresu


1 Answers

This will fix the issue as your method expects a function that returns IInterface

public class Program
{
    public static void Main(string[] args)
    {
        Method(() => GetObject());
    }

    public static IInterface GetObject() => null;

    public static void Method(Func<IInterface> func) =>
        Console.WriteLine("First");

    public static void Method(Func<Task<IInterface>> funcAsync) =>
        Console.WriteLine("Second");
}

public interface IInterface { }
like image 89
ogomrub Avatar answered Jan 27 '26 06:01

ogomrub



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!