首页 > 解决方案 > 应为 C# 方法名称,尝试通过函数调用解析数字

问题描述

我试图调用一个函数“disp”并通过数字 1 进行解析,但有问题指出方法名称是预期的。如果我能理解如何解析一个数字和 threadstart 中的函数,那就太棒了。先感谢您

错误图片

    class Class1
    {
        public static void disp(int num)
        {

                try
                {
                    Console.WriteLine(num);
                    Thread.Sleep(500);
                }
                catch (Exception e)
                {
                    Console.WriteLine("ERROR");
                }

            Console.WriteLine("Done");


        }

        public static void Main(string[] args)
        {
            ThreadStart ts1 = new ThreadStart(disp(1));
            Thread t = new Thread(ts1);
            t.Start();
            Console.ReadLine();

        }
    }
}

标签: c#

解决方案


如果要将参数传递给静态方法,则需要使用ParamaterizedThreadStart

class Class1
{
    public static void Disp(object num)
    {

            try
            {
                Console.WriteLine((int)num);
                Thread.Sleep(500);
            }
            catch (Exception e)
            {
                Console.WriteLine("ERROR");
            }

        Console.WriteLine("Done");


    }

    public static void Main(string[] args)
    {
        Thread t = new Thread(Class1.Disp);
        t.Start(1);
        Console.ReadLine();

    }
}

}


推荐阅读