Domanda

The Thread class has 4 different constructores:

Thread(ParameterizedThreadStart)

Thread(ThreadStart)

Thread(ParameterizedThreadStart, Int32)

Thread(ThreadStart, Int32)

But why this following code works?

class Program {
    static void Main(string[] args) {

        Thread t = new Thread(count2);
        t.Start();
        count1();
        Console.ReadLine();
    }

    public static void count1() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("count1: " + i);
        }
    }

    public static void count2() {
        for (int i = 0; i < 10; i++) {
            Console.WriteLine("count2: " + i);
        }
    }
}

I don't pass the Thread constructore a ParameterizedThreadStart-delegate or a ThreadStart-delegate. I pass only a normal count2 method. But why this works?

È stato utile?

Soluzione

That is because the compiler automatically adds a delegate construction for you, and it infers the type of delegate.

This page documents the exact feature.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top