c# - Return types within a Task running an async anonymous function? -


i have function returns task<string>, construct task have use anonymous async function because must await multiple calls within task. have found can return "" though if try return null error arises (visual studio message)

anonymous function converted void returning delegate cannot return value

async lambda expression converted 'task' returning delegate cannot return value. did intend return 'task'?

a function showing same issue

public virtual task<string> foobar() {     return task<string>.run(async () =>     {         await task.delay(1500);         return ""; // ok         //return null; // error      }); } 

what happening here?

would more appropriate return await task.fromresult<string>(null); if return null value?

the compiler not know null string's null , therefor can't automatically choose correct type func<task<string>>. following instead tell compiler null string.

return (string)null; 

also, brought in comments, task.run<tresult>(func<task<tresult>> function) static method when task<string>.run(... still calling same static method task.run no information. need instead call , pass type in run portion forcing return type string instead of making compiler try , figure out return type should be.

public virtual task<string> foobar() {     return task.run<string>(async () =>     {         await task.delay(1500);         return null; // not error      }); } 

Comments