简体   繁体   English

为什么 VS2017 说 await 是一个未知的关键字?

[英]Why does VS2017 say await is an unknown keyword?

    internal /*async*/ void RunAsync()
    {
        var tasks = ...;
        await Task.WhenAll(tasks);
    }

In the above example, the IDE informs me "the ''await operator can only be used within an async method".在上面的示例中,IDE 通知我“''await 运算符只能在异步方法中使用”。 It offers me fixes to make my method signature async void or async Task它为我提供了使我的方法签名async voidasync Task修复程序

    internal /*async*/ void RunAsync()
    {
        var tasks = ...;
        var t = Task.WhenAll(tasks);
        await t;
    }

In the second example, it gives the error "the type or namespace await cannot be found"... await is not color-coded as a language keyword and the IDE offers me no automatic fixes.在第二个示例中,它给出了错误“找不到类型或命名空间 await”... await没有作为语言关键字进行颜色编码,IDE 没有提供自动修复。

Is this a VS bug?这是一个VS错误吗?

The code is invalid C# code in both cases.在这两种情况下,代码都是无效的 C# 代码。 So the compiler/IDE has to do its best to figure out what you're trying to do and give you an appropriate error message.所以编译器/ IDE已经尽力找出你正在试图做的,给你相应的错误信息是什么。

In the above example, the IDE informs me "the ''await operator can only be used within an async method".在上面的示例中,IDE 通知我“''await 运算符只能在异步方法中使用”。 It offers me fixes to make my method signature async void or async Task它为我提供了使我的方法签名 async void 或 async Task 的修复程序

VS is parsing the code and is pretty sure that the await here is intended to be the await operator. VS 正在解析代码,并且非常确定这里的await旨在成为await运算符。 The code isn't valid C#, but that's what it looks like you're trying to do.该代码不是有效的 C#,但这就是您要尝试执行的操作。 So it gives you a helpful suggestion that you have to have the async keyword to make await a keyword.因此,它为您提供了一个有用的建议,即您必须使用async关键字才能使await成为关键字。

In the second example, it gives the error "the type or namespace await cannot be found"... await is not color-coded as a language keyword and the IDE offers me no automatic fixes.在第二个示例中,它给出了错误“找不到类型或命名空间 await”...await 没有作为语言关键字进行颜色编码,IDE 没有提供自动修复。

VS is parsing the code and is not sure whether the await here is an await operator or a type. VS 正在解析代码,不确定这里的awaitawait运算符还是类型。 await t; looks a lot like int t;看起来很像int t; to the compiler.到编译器。

Is this a VS bug?这是一个VS错误吗?

It's not a bug, since the code is invalid C# to begin with.这不是错误,因为代码一开始就是无效的 C#。 But you could ask for a better error message in the second case, and the C# compiler team may or may not decide it's worth doing that instead of working on other features.但是在第二种情况下,您可以要求提供更好的错误消息,C# 编译器团队可能会也可能不会决定这样做而不是处理其他功能是值得的。

For context, note that await is a contextual keyword .对于上下文,请注意await上下文关键字 It's only a keyword if the async keyword is present.如果存在async关键字,它只是一个关键字。 await is not always a keyword, and it would be a bug if VS were to always treat it as a keyword. await并不总是一个关键字,如果 VS 总是把它当作一个关键字,那将是一个错误。 Specifically, in both the code examples here, await is not a keyword.具体来说,在这里的两个代码示例中, await都不是关键字。 VS goes out of its way to do extra fallback parsing in the first example to detect a common scenario where the developer intended to use the await keyword. VS 在第一个示例中不遗余力地进行额外的回退解析,以检测开发人员打算使用await关键字的常见场景。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM