有人能告诉我这两个代码片段在 .NET 8 中是否功能等效吗?
如果不是,为什么不是?
第一个片段
Task<ICollection<SearchResults>> getResultTask = _aRepo.List(request.AParam, request.PageIndex, request.PageSize);
if (request.PageIndex != null && request.PageSize != null)
{
int totalRecords = await _myRepo.CountAsync();
// store totalrecords (non-async)
}
ICollection<SearchResults> results = await getResultTask;
第二个片段:
if (request.PageIndex != null && request.PageSize != null)
{
int totalRecords = await _myRepo.CountAsync();
// store totalrecords (non-async)
}
// Is this functionally equivalent to the above?
ICollection<SearchResults> results = await _aRepo.List(request.AParam, request.PageIndex, request.PageSize);
考虑以下代码:
现在如果你从控制台应用程序调用它,如下所示:
这将输出大约 3 秒的时间,因为
method1()
在启动和等待之前已method2()
启动且未等待。method1()
大约需要 2 秒,method2()
大约需要 3 秒,因此总时间将是这两个时间中较长的一个。现在看看如果你这样做会发生什么:
这将输出大约 5 秒,因为
method1()
已启动并等待,大约需要 2 秒才能完成。只有在method2()
启动并等待之后,大约需要 3 秒才能完成。因此总时间为 5 秒(而第一个例子为 3 秒)。