So I've got this bit of code that Visual Studio doesn't complain about:
int fortyTwo = await CalculateAnswerAsync();
Debug.WriteLine(fortyTwo);
async public static Task<int> CalculateAnswerAsync()
{
await Task.Delay(1000);
return 42;
}
I'm returning a task, which encapsulates the int 42 that I'm returning after 1 second and accessing via await. All great.
Now when I try to apply this methodology to an area of my program dealing with Lists, I get an error:
public List<Payee> Payees { get; set; } = new();
var payeesTask = dataService.GetPayees();
List<Payee> payees = await payeesTask;
Debug.WriteLine(payees.ToString());
public Task<List<Payee>> GetPayees()
{
//Payees is underlined with the red squiggle, boooo
if(Payees.Count != 0) return Payees;
Payees.Add(new Payee { Id = 0, Name = "Food Lion", DefaultCategoryId = 0, DefaultIsCredit = false });
return Payees;
}
The error I get is Cannot implicitly convert type 'System.Collections.Generic.List<MyApp.Model.Payee>' to 'System.Threading.Tasks.Task<System.Collections.Generic.List<MyApp.Model.Payee>>'
Which I totally understand and agree with, except that my example with returning 42 doesn't error out. What gives?