26

Both of the following lines work same. but is there any hidden difference? and which one should be preferred?

Thread t1 = new Thread(aMethod);

Thread t2 = new Thread(new ThreadStart(aMethod));

Thanks.

Eamon Nerbonne
  • 47,023
  • 20
  • 101
  • 166
Azodious
  • 13,752
  • 1
  • 36
  • 71
  • 2
    Specifying a language tag may get this question more attention. C#? – Bert F Feb 01 '11 at 12:11
  • Check out this post [Why does new Thread() accept a method name, even though none of the constructor overloads seem to allow this](http://stackoverflow.com/questions/12595582/why-does-new-thread-accept-a-method-name-even-though-none-of-the-constructor) – broadband May 12 '14 at 10:12
  • possible duplicate of [C# Delegate Instantiation vs. Just Passing the Method Reference](http://stackoverflow.com/questions/2181282/c-sharp-delegate-instantiation-vs-just-passing-the-method-reference) – nawfal Jul 06 '14 at 20:27

4 Answers4

15

There is no difference. Both lines are equal.

gor
  • 11,498
  • 5
  • 36
  • 42
  • I know there is no difference (tried it) but it would be desirable to support it with any additional links to documentation, references ... – broadband May 12 '14 at 10:06
6

The c# compiler will transform the Thread t1 = new Thread(aMethod); statement to Thread t2 = new Thread(new ThreadStart(aMethod));

Vlad
  • 83
  • 4
3

They are just the same but the second one allows you to use an extra parameter at the Thread starting method (well using ParametrizedThreadStart instead of ThreadStart).

Ignacio Soler Garcia
  • 21,122
  • 31
  • 128
  • 207
0

A ThreadStart represents the method that executes on a Thread, so this is exactly the same thing.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Stephane Halimi
  • 408
  • 3
  • 5