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

≯℡__Kan透↙ 提交于 2019-12-21 04:25:27

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/9117514/should-methods-in-an-api-that-return-task-end-with-task-or-async

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!