API中返回Task的方法是否应以Task或Async结尾 [英] Should methods in an API that return Task end with Task or Async
问题描述
如果API具有同步方法T DoSomething<T>();
,则返回Task<T>
时对应的异步方法的命名约定是什么?
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>();
或
Task<T> DoSomethingAsync<T>();
还是其他?
推荐答案
根据我在C#5/.NET 4.5中看到的内容,首选名称是DoSomethingTaskAsync
或DoSomethingAsync
From what I've seen in C# 5 / .NET 4.5, the preferred name is DoSomethingTaskAsync
or DoSomethingAsync
例如, .NET 4.5中的WebClient 类具有类似DownloadFileTaskAsync
的方法,因为它已经有一个名为DownloadFileAsync
的方法,因此我认为在Async
上使用TaskAsync
是为了保持向后兼容性.
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.
这篇关于API中返回Task的方法是否应以Task或Async结尾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!