C# 代表的类型转换

C# 代表的类型转换,c#,delegates,C#,Delegates,我已经创建了具有为线程类作业传递的相同签名函数的委托: public delegate void myDelegateForThread(); static void Main(string[] args) { myDelegateForThread del = thrFunc; Thread t = new Thread( del); t.Start(); for (int i = 0; i &l

我已经创建了具有为线程类作业传递的相同签名函数的委托:

    public delegate void myDelegateForThread();

    static void Main(string[] args)
    {
        myDelegateForThread del = thrFunc;


        Thread t = new Thread( del);
        t.Start();


        for (int i = 0; i < 10000; i++) { Console.WriteLine("Main thread " +i); }

        Console.ReadLine();

    }

    public static void thrFunc()
    {
        for (int i = 0; i < 10000; i++) { Console.WriteLine("Secondary thread " + i); }
    }
public委托void myDelegateForThread();
静态void Main(字符串[]参数)
{
myDelegateForThread del=thrFunc;
螺纹t=新螺纹(del);
t、 Start();
对于(inti=0;i<10000;i++){Console.WriteLine(“主线程”+i);}
Console.ReadLine();
}
公共静态void thrFunc()
{
对于(inti=0;i<10000;i++){Console.WriteLine(“次线程”+i);}
}

但编译器不高兴我没有通过
ThreadStart
Thread
构造函数。我知道如何解决这种情况下的问题,但我的问题是,如果代表具有相同的签名,我可以为他们进行打字吗?

如果您的意思是,不,它不起作用:

public delegate int MyDelegate1(double param);
public delegate int MyDelegate2(double param);
public int MyFunction(double p) { return 1; }

MyDelegate1 del1 = MyFunction;
MyDelegate2 del2 = (MyDelegate2)del1;
委托的方式与任何其他类型相同,并且没有继承或接口关系->即使签名匹配,它们也不能相互类型转换

工作原理是将其包装到新的委托中:

MyDelegate2 del2 = new MyDelegate2(del1);

如果您是指以下情况,则不适用:

public delegate int MyDelegate1(double param);
public delegate int MyDelegate2(double param);
public int MyFunction(double p) { return 1; }

MyDelegate1 del1 = MyFunction;
MyDelegate2 del2 = (MyDelegate2)del1;
委托的方式与任何其他类型相同,并且没有继承或接口关系->即使签名匹配,它们也不能相互类型转换

工作原理是将其包装到新的委托中:

MyDelegate2 del2 = new MyDelegate2(del1);

请向我们展示如何声明
myDelegateForThread
。我已经用delegated更新了代码你说“编译器不高兴”是什么意思?你到底得到了什么错误?使用thrFunc作为ThreadStart参数创建线程应该不会有任何问题。请告诉我们如何声明
myDelegateForThread
。我已经用delegated更新了代码你说“编译器不高兴”是什么意思?你到底得到了什么错误?使用thrFunc作为ThreadStart参数创建线程应该不会有任何问题。