“System.threading.threadStart'是'类型',在给定的上下文中无效'?(C#)

时间:2012-12-16 12:51:37

标签: c# .net multithreading delegates lambda

我在var ts = System.Threading.ThreadStart(delegate()上收到了错误(红线已经淹没在System.Threading.ThreadStart下)。问题是什么?

using System;
using System.Threading;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        public Program()
        {
            int[] iArray = new int[3];
            iArray[0] = 2;
            iArray[1] = 1;
            iArray[2] = 5;
            var ts = System.Threading.ThreadStart(delegate()
            {
                foreach (int i in iArray)
                    Foo(i);
            });
        }

        public void Foo(int i)
        {
            Console.WriteLine(i + ",");
        }

        public static void Main(String[] args)
        {
            Program p = new Program();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

您错过了new

var ts = new System.Threading.ThreadStart(delegate()
         {
             foreach (int i in iArray)
                 Foo(i);
         });

顺便说一句:

您不必在ThreadStart前加上其名称空间System.Threading,因为您的* .cs文件顶部已经有using声明。