Abu*_*mud 2 c# using asp.net-core .net-5
我有一个非常奇怪的编译时错误,我在谷歌中检查的任何地方我都看到我的语法应该绝对没问题。
我的项目是 .net-5 并用 C# 语言编写。
public async Task Init()
{
using IEngineProviderAsync provider = await CreateProviderAsync();
}
Run Code Online (Sandbox Code Playgroud)
CS8418“IEngineProviderAsync”:using 语句中使用的类型必须可隐式转换为“system.idisposable”。您的意思是“等待使用”而不是“使用”吗?
我搜索了 CS8418 但没有数据。微软链接不起作用https://docs.microsoft.com/en-us/dotnet/csharp/misc/sorry-we-don-t-have-specifics-on-this-csharp-error?f1url=% 3FappId%3Droslyn%26k%3Dk(CS8418)
它不为您编译的原因是它似乎IEngineProviderAsync实现了IAsyncDisposable。
您需要等待接口的DisposeAsync方法,IAsyncDisposable这就是为什么您需要等待 using 语句,因为错误提示 -
did you mean 'await using' rather than 'using'?。
await using IEngineProviderAsync provider = await CreateProviderAsync();
Run Code Online (Sandbox Code Playgroud)
点击这里理解之间的差异using和await using。