您好,我正在尝试了解任务和异步方法的概念。我一直在玩这个代码一段时间无济于事。有人能告诉我如何从 test() 方法中获取返回值并将该值分配给变量吗?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Task test1 = Task.Factory.StartNew(() => test());
System.Console.WriteLine(test1);
Console.ReadLine();
}
public static async Task<int> test()
{
Task t = Task.Factory.StartNew(() => { Console.WriteLine("do stuff"); });
await t;
return 10;
}
}
}
Run Code Online (Sandbox Code Playgroud)