Task.FromResult用法以及作用

Task.FromResult用于返回带有值并已经完成的Task

常用的场景:

1.以同步实现异步方法(指定要返回的值为定值)

public Task<int> DoSthAsync()
    {
        int result = 1;
        return Task.FromResult(result);
    }

2.从缓存中取值(可能从本地缓存中取,此时为同步)

如果不带返回值,使用Task.FromResult(0)或者Task.FromResult(null)

具体解释查看:

https://www.cnblogs.com/stilldream/p/10184778.html

记录编程的点滴,体会学习的乐趣
原文地址:https://www.cnblogs.com/AduBlog/p/13520328.html