有没有像这样编写方法的场景:
public async Task<SomeResult> DoSomethingAsync()
{
// Some synchronous code might or might not be here... //
return await DoAnotherThingAsync();
}
Run Code Online (Sandbox Code Playgroud)
而不是这个:
public Task<SomeResult> DoSomethingAsync()
{
// Some synchronous code might or might not be here... //
return DoAnotherThingAsync();
}
Run Code Online (Sandbox Code Playgroud)
会有意义吗?
为什么return await在可以直接Task<T>从内部DoAnotherThingAsync()调用返回时使用构造?
我return await在很多地方看到代码,我想我应该错过一些东西.但据我了解,在这种情况下不使用async/await关键字并直接返回Task将在功能上等效.为什么要增加附加await层的额外开销?
我很好奇直接调用Func和使用Invoke()之间的区别.有区别吗?是第一个,语法糖,并在下面调用Invoke()?
public T DoWork<T>(Func<T> method)
{
return (T)method.Invoke();
}
Run Code Online (Sandbox Code Playgroud)
VS
public T DoWork<T>(Func<T> method)
{
return (T)method();
}
Run Code Online (Sandbox Code Playgroud)
或者我完全走错了轨道:)谢谢.