-1

What is the difference between ThreadStart delegate passing to Thread constructor in code below:

class Program
{
    public static void ThreadMethod()
    {
    //...
    }
    static void Main(string[] args)
    {

        Thread t = new Thread(ThreadMethod);
        Thread t = new Thread(new ThreadStart(ThreadMethod));

        t.Start();
    }
}

In both cases program works in same way. Why I should create new delegate object and call ThreadMethod delegate constructor?

vico
  • 17,051
  • 45
  • 159
  • 315
  • `To start a thread using an instance method for the thread procedure, use the instance variable and method name when you create the ThreadStart delegate. Beginning in version 2.0 of the .NET Framework, the explicit delegate is not required.` This is a comment in a MS example. It looks like it syntax from pre .NET 2.0 – Mark Baijens Jan 22 '19 at 09:44

2 Answers2

1

There is no difference it's pure syntactic sugar. The compiler knows the type of delegate that is expected and creates it automatically.

Here you create delegate yourself:

Thread t = new Thread(new ThreadStart(ThreadMethod));

And in this example compiler creates it automatically:

// The type of delegate is inferred by compiler and delegate is created
Thread t = new Thread(ThreadMethod);
Fabjan
  • 13,506
  • 4
  • 25
  • 52
0

There's no difference. In the first case the compiler will silently create the delegate for you, passing in your ThreadMethod:

Thread t = new Thread(ThreadMethod);

will become :

Thread t = new Thread(new ThreadStart(ThreadMethod));
Sean
  • 60,939
  • 11
  • 97
  • 136