API中返回Task的方法是否应以Task或Async结尾 [英] Should methods in an API that return Task end with Task or Async

查看:125
本文介绍了API中返回Task的方法是否应以Task或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中看到的内容,首选名称是DoSomethingTaskAsyncDoSomethingAsync

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆