Home » c# » c# – Func syntax for async call

c# – Func syntax for async call

Posted by: admin February 21, 2020 Leave a comment

Questions:

I try to create an AsyncCommand with a parameter.

I used this method but it’s not working.

 public static AsyncCommand<T> Create(Func<System.Threading.Tasks.Task<T>> func)
    {

        return new AsyncCommand<T>(new Command<T>(async (x) =>  await func(x)));
    }

And when I call it with my viewmodel:

 public ICommand OnRemoveTagCommand = AsyncCommand<ResultElementRatingDto>.Create(RemoveTag);

 private async Task<ResultElementRatingDto> RemoveTag(ResultElementRatingDto ratingDto)
    {

        return null;
    }

The error is:

cannot convert from ‘method group’ to ‘Func’

What’s wrong in my code?

How to&Answers:

Depending on the overloads of Create that are available, and depending on the language version you’re using, the compiler sometimes cannot resolve a method group to an unambiguous overload when only considering return types. In that case, call the method with a lambda expression:

public ICommand OnRemoveTagCommand = AsyncCommand<ResultElementRatingDto>.Create(x => RemoveTag(x));