简体   繁体   English

应该返回Task的API中的方法是否以Task或Async结尾

[英]Should methods in an API that return Task end with Task or Async

If an API has a synchronous method T DoSomething<T>(); 如果API具有同步方法T DoSomething<T>(); , what is the naming convention for the corresponding asynchronous method if it returns Task<T> ? ,如果返回Task<T> ,相应的异步方法的命名约定是什么?

Task<T> DoSomethingTask<T>();    

or 要么

Task<T> DoSomethingAsync<T>();

or something else? 或者是其他东西?

If you are talking about an async or await method, then from MSDN : 如果您正在讨论asyncawait方法,那么从MSDN

By convention, the suffix "Async" is added to the names of methods that are modified by the Async or async modifier. 按照惯例,后缀“Async”将添加到由Async或async修饰符修改的方法的名称中。

... ...

Exceptions to the convention can be made where an event, base class, or interface contract suggests a different name. 可以在事件,基类或接口契约建议不同名称的情况下对约定进行例外处理。 For example, the names of common event handlers, such as button1_Click, are best not renamed. 例如,最好不重命名常用事件处理程序的名称,例如button1_Click。

From what I've seen in C# 5 / .NET 4.5, the preferred name is DoSomethingTaskAsync or DoSomethingAsync 从我在C#5 / .NET 4.5中看到的,首选名称是DoSomethingTaskAsyncDoSomethingAsync

For example, the WebClient class in .NET 4.5 has methods like DownloadFileTaskAsync , because it already had a method named DownloadFileAsync , so I assume the use of TaskAsync over Async is to maintain backwards compatibility. 例如,.NET 4.5中的WebClient类具有类似DownloadFileTaskAsync方法,因为它已经有一个名为DownloadFileAsync的方法,所以我假设在Async使用TaskAsync是为了保持向后兼容性。

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

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