在方法名称后缀“Async”的约定是什么?
是否应该将“Async”后缀仅附加到使用async
声明的方法?修饰符?
public async Task<bool> ConnectAsync()
或者该方法只返回 Task<T>
就足够了吗?或 Task
?
public Task<bool> ConnectAsync()
最佳答案
我认为即使从 Microsoft 文档来看,事实也是模棱两可的:
In Visual Studio 2012 and the .NET Framework 4.5, any method that is attributed with the
async
keyword (Async
in Visual Basic) is considered an asynchronous method, and the C# and Visual Basic compilers perform the necessary transformations to implement the method asynchronously by using TAP. An asynchronous method should return either aTask
or aTask<TResult>
object.
http://msdn.microsoft.com/en-us/library/hh873177(v=vs.110).aspx
这已经不对了。任何带有 async
的方法是异步的然后它说它应该返回 Task
或 Task<T>
- 这不适用于调用堆栈顶部的方法,例如 Button_Click 或 async void
.
当然,你要考虑约定的意义何在?
你可以说 Async
后缀约定是为了向 API 用户传达该方法是可等待的。对于可等待的方法,它必须返回 Task
对于无效,或 Task<T>
对于返回值的方法,这意味着只有后者可以以 Async
为后缀.
或者您可以说 Async
后缀约定是为了传达该方法可以立即返回,放弃当前线程以执行其他工作并可能导致竞争。
这个 Microsoft 文档引用说:
By convention, you append "Async" to the names of methods that have an Async or async modifier.
Content now only available via the Wayback Machine
甚至没有提到您自己的异步方法返回 Task
需要 Async
后缀,我想我们都同意他们这样做。
所以这个问题的答案可能是:两者都有。在这两种情况下,您都需要附加 Async
使用 async
的方法关键字并返回 Task
或 Task<T>
.
我要请 Stephen Toub 澄清情况。
更新
所以我做到了。这是我们的好人写的:
If a public method is Task-returning and is asynchronous in nature (as opposed to a method that is known to always execute synchronously to completion but still returns a Task for some reason), it should have an “Async” suffix. That’s the guideline. The primary goal here with the naming is to make it very obvious to a consumer of the functionality that the method being invoked will likely not complete all of its work synchronously; it of course also helps with the case where functionality is exposed with both synchronous and asynchronous methods such that you need a name difference to distinguish them. How the method achieves its asynchronous implementation is immaterial to the naming: whether async/await is used to garner the compiler’s help, or whether types and methods from System.Threading.Tasks are used directly (e.g. TaskCompletionSource) doesn’t really matter, as that doesn’t affect the method’s signature as far as a consumer of the method is concerned.
Of course, there are always exceptions to a guideline. The most notable one in the case of naming would be cases where an entire type’s raison d’etre is to provide async-focused functionality, in which case having Async on every method would be overkill, e.g. the methods on Task itself that produce other Tasks.
As for void-returning asynchronous methods, it’s not desirable to have those in public surface area, since the caller has no good way of knowing when the asynchronous work has completed. If you must expose a void-returning asynchronous method publicly, though, you likely do want to have a name that conveys that asynchronous work is being initiated, and you could use the “Async” suffix here if it made sense. Given how rare this case should be, I’d argue it’s really a case-by-case kind of decision.
I hope that helps, Steve
斯蒂芬开场白的简洁指导已经足够清楚了。它不包括 async void
因为想要创建具有这种设计的公共(public) API 是不寻常的,因为实现异步 void 的正确方法是返回一个普通的 Task
实例并让编译器发挥它的魔力。但是,如果您确实想要 public async void
, 然后追加 Async
建议。其他栈顶 async void
诸如事件处理程序之类的方法通常不公开并且无关紧要/不合格。
对我来说,它告诉我,如果我发现自己想知道后缀 Async
在 async void
上, 我可能应该把它变成一个 async Task
以便调用者可以等待它,然后附加 Async
.
关于c# - 方法名中使用 "Async"后缀是否取决于是否使用 'async'修饰符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15951774/