简体   繁体   中英

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

If an API has a synchronous method T DoSomething<T>(); , what is the naming convention for the corresponding asynchronous method if it returns 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 :

By convention, the suffix "Async" is added to the names of methods that are modified by the Async or async modifier.

...

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.

From what I've seen in C# 5 / .NET 4.5, the preferred name is DoSomethingTaskAsync or DoSomethingAsync

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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