I have the following code:
DateTime start = DateTime.Now;
Func<Task<double>> f = async () =>
{
await Task.Delay(3000, cts.Token);
return (DateTime.Now - start).TotalMilliseconds;
};
Task<double> delayTask = f();
But I can't figure it out how to write it as a single statement without assigning the delegate to a variable first. That is, I feel like I should be able to just do something like
Task<double> delayTask = (async () =>
{
await Task.Delay(3000, cts.Token);
return (DateTime.Now - start).TotalMilliseconds;
})();
or even
Task<double> delayTask = new Task<double>(async () =>
{
await Task.Delay(3000, cts.Token);
return (DateTime.Now - start).TotalMilliseconds;
});
but both give compilation errors. What am I missing?