什么是Task.FromResult&LT使用; TResult>在C#中 [英] What is the use for Task.FromResult<TResult> in C#

查看:1607
本文介绍了什么是Task.FromResult&LT使用; TResult>在C#中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#和TPL(任务并行库),在任务类代表产生类型T的值的一个正在进行的工作。

In C# and TPL (Task Parallel Library), the Task class represents an ongoing work that produces a value of type T.

我想知道什么是需要的<一个HREF =http://msdn.microsoft.com/en-us/library/hh194922.aspx> Task.FromResult 方法?

I'd like to know what is the need for the Task.FromResult method ?

这是:?在这样一个场景,你已经有了手头的生产价值,是什么来包装它放回任务的需要。

That is: In a scenario where you already have the produced value at hand, what is the need to wrap it back into a Task?

这想到的唯一的事情就是。它被用来作为一些适配器用于其他方法接受一个任务实例

The only thing that comes to mind is that it's used as some adapter for other methods accepting a Task instance.

推荐答案

有,我发现了两个常见的用例:

There are two common use cases I've found:


  1. 当你实现一个接口,它允许异步调用者,但您的实现是同步的。

  2. 当你斯塔宾/嘲讽异步代码进行测试。

这篇关于什么是Task.FromResult&LT使用; TResult&GT;在C#中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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