闭包和任务
这些函数调用之间是否存在功能差异?
方法一:
public static void PrintMe(object obj)
{
Task task = new Task(() =>
{
Console.WriteLine(obj.ToString());
});
task.Start();
}
方法二:
public static void PrintMe(object obj)
{
Task task = new Task((object arg) =>
{
Console.WriteLine(arg.ToString());
}, obj);
task.Start();
}
Is there any functional difference between these to function calls.
Method1:
public static void PrintMe(object obj)
{
Task task = new Task(() =>
{
Console.WriteLine(obj.ToString());
});
task.Start();
}
Method2:
public static void PrintMe(object obj)
{
Task task = new Task((object arg) =>
{
Console.WriteLine(arg.ToString());
}, obj);
task.Start();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
第一个将变量 obj 传递给任务。第二个传递 obj 的值。
要查看差异,请在创建任务后为 obj 指定其他内容。
The first one passes the variable obj to the task. The second one passes the value of obj.
To see the difference assign something else to obj after creating the task.