Home > database >  delegate and the await task
delegate and the await task

Time:05-31

creating method delegates and I ran into one problem:

D_AmortizationCalsAsync d_AmortizationCalsAsync = AmortizationCalsAsync; // here i have error

public delegate Func<Task<decimal>> D_AmortizationCalsAsync(int amortization, int installmentNumber);

public async Task<decimal> AmortizationCalsAsync(int amortization, int installmentNumber)
    {
      (...)
    }

Tak AmortizationCalsAsync (int, int) has a wrong return type

https://docs.microsoft.com/en-us/dotnet/csharp/misc/cs0407?f1url=?appId=roslyn&k=k(CS0407)

CodePudding user response:

AmortizationCalsAsync returns a Task<decimal>, not a Func<Task<decimal>>. If you want to assign it to delegate you need either change type of the delegate:

public delegate Task<decimal> D_AmortizationCalsAsync(int amortization, int installmentNumber);

Or use anonymous lambda to create a function with correct signature and return type (i.e. a function accepting 2 integers and returning a func with no parameters and Task<decimal> return type):

D_AmortizationCalsAsync d_AmortizationCalsAsync = 
    (a, i) => () => AmortizationCalsAsync(a, i);
  • Related