Should methods in an API that return Task end with

2019-03-27 13:11发布

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?

2条回答
Bombasti
2楼-- · 2019-03-27 13:22

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.

查看更多
Root(大扎)
3楼-- · 2019-03-27 13:31

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.

查看更多
登录 后发表回答