返回值的C#任务 [英] C# Task which returns a value
本文介绍了返回值的C#任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试在任务中运行功能,但是我做错了事. 这是一个例子:
I'm trying to run a function in a task but I'm doing something wrong. Heres an example:
var t = Task<int>.Factory.StartNew(() => GenerateResult(2));
static int GenerateResult(int i)
{
return i;
}
最后Console.WriteLine(t);
这将返回:
System.Threading.Tasks.Task`1 [System.Int32]
System.Threading.Tasks.Task`1[System.Int32]
我希望我成为2岁. 我在这里做错了什么? :/
I want i to be 2. What am I doing wrong here? :/
推荐答案
您正在打印创建的任务对象.有关结果,请参见.Result
属性:
You are printing the task object that you created. For result, see .Result
property:
Console.WriteLine(t.Result);
这篇关于返回值的C#任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文