我在这里使用示例表单 asp.net
所以,我问我,两个代码有什么区别:
public class ServiceTest
{
public Task<List<Widget>> WidgetsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var widgetService = new WidgetService();
return widgetService.GetWidgetsAsync(cancellationToken);
}
public Task<List<Product>> ProductAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var prodService = new ProductService();
return prodService.GetProductsAsync(cancellationToken);
}
public Task<List<Gizmo>> GizmoAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var gizmoService = new GizmoService();
return gizmoService.GetGizmosAsync(cancellationToken);
}
}
和
public class ServiceTest
{
public async Task<List<Widget>> WidgetsAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var widgetService = new WidgetService();
return await widgetService.GetWidgetsAsync(cancellationToken);
}
public async Task<List<Product>> ProductAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var prodService = new ProductService();
return await prodService.GetProductsAsync(cancellationToken);
}
public async Task<List<Gizmo>> GizmoAsync(CancellationToken cancellationToken = default(CancellationToken))
{
var gizmoService = new GizmoService();
return await gizmoService.GetGizmosAsync(cancellationToken);
}
}
经过的时间对我来说完全一样..我从异步开始,所以也许这是一个愚蠢的问题,但我想在采取错误的方向之前确定:)