几道C#题

一、以下代码共创建了几个Task实例?

var tasks = Enumerable.Range(1, 2).Select(i => new Task(() =>
{
    Debug.WriteLine(i);
})).ToList();
var ts1 = tasks.ToList();
var ts2 = tasks.ToArray();



二、找错误:

var tasks = Enumerable.Range(1, 2).Select(i => new Task(() =>
{
    Debug.WriteLine(i);
}));
tasks.ToList().ForEach(t => t.Start());
Task.WaitAll(tasks.ToArray());
Console.WriteLine("完毕");



三、以下代码在vs2010中的执行结果是?

var sequence = Enumerable.Range(1, 2).ToList();

sequence.Select(i => new Action(() => Debug.WriteLine(i))).ToList().ForEach(a => a());

Action prints = null;
foreach (var i in sequence)
{
    prints += () => Debug.WriteLine(i);
}
prints();

VS2012解决了foreach与linq(Select)的闭包BUG。

posted @ 2012-11-29 11:48  beta2013  阅读(151)  评论(0编辑  收藏  举报