gpt4 book ai didi

c# - 为什么在 finally block 中不允许等待?

转载 作者:可可西里 更新时间:2023-11-01 09:05:20 26 4
gpt4 key购买 nike

为什么 await 不允许出现在 finally block 中?

public async void Fn()
{
try
{
}
finally
{
await Task.Delay(4000);
}
}

知道可以手动获取Awaiter

public void Fn()
{
try
{
}
finally
{
var awaiter = Task.Delay(4000).GetAwaiter();
}
}

最佳答案

取自:Where can’t I use “await”?

Inside of a catch or finally block. You can use “await” inside of a try block, regardless of whether it has associated catch or finally blocks, but you can’t use it inside of the catch or finally blocks. Doing so would disrupt the semantics of CLR exception handling.

这显然在 C# 6.0 中不再适用

取自:A C# 6.0 Language Preview

C# 6.0 does away with this deficiency, and now allows await calls within both catch and finally blocks (they were already supported in try blocks)

关于c# - 为什么在 finally block 中不允许等待?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14646563/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com