6

Possible Duplicate:
What is the difference between new Thread(void Target()) and new Thread(new ThreadStart(void Target()))?

I have a small question about Thread class. This class has 4 constructors:

public Thread(ParameterizedThreadStart start);
public Thread(ThreadStart start);
public Thread(ParameterizedThreadStart start, int maxStackSize);
public Thread(ThreadStart start, int maxStackSize);

I use the 2nd constructor to create a Thread object:

Thread thread = new Thread(new ThreadStart(ScanDirectory));

However, I can use a way to create this object without using any constructors I talk above.

Thread thread = new Thread(ScanDirectory);

In this case, ScanDirectory is a void method, it isn't ThreadStart or ParameterizedThreadStart but Thread class still accepts this constructor. Why? I think this is a .NET feature but I don't know how it's implemented.

Note: ScanDirectory is a void method.

Community
  • 1
  • 1
Leo Vo
  • 9,980
  • 9
  • 56
  • 78
  • `public delegate void ThreadStart()` it represents the methods to be invoked when this thread begins executing – V4Vendetta Sep 26 '12 at 06:27
  • It's a C# 2.0 feature called implicit method group conversions, that allows you to omit the explicit construction of a new delegate instance here. – CodesInChaos Sep 26 '12 at 06:28

4 Answers4

9

It's important to separate two things here:

  • The call to the Thread constructor
  • The creation of a delegate to pass to the Thread constructor

You're really interested in the latter here - the difference between:

ThreadStart tmp = new ThreadStart(ScanDirectory);

and

ThreadStart tmp = ScanDirectory;

The second of these is a method group conversion - an implicit conversion from a method group (the name of a method, possibly qualified by an instance value if it's an instance method) to a delegate with a compatible signature.

You very rarely need the "explicit" delegate creation expression of the first form, since method group conversions were introduced in C# 2. You'll see a lot of code which still uses it because many developers are unaware of method group conversions, unfortunately - and IIRC the Visual Studio designers still uses that form for event handler subscription.

The only time you'd really need it was when the method group conversion ended up being ambiguous. For example:

static void Main()
{
    // Valid: uses parameterless overload
    new Thread(new ThreadStart(Foo));

    // Valid: uses parameterized overload
    new Thread(new ParameterizedThreadStart(Foo));

    // Invalid, as there are two valid constructor overloads
    new Thread(Foo);
}

static void Foo()
{
}

static void Foo(object state)
{
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • On the "compatibility" aspect: it took me a while to figure out that the [signature of ThreadStart](https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadstart) _is_ the compatibility pattern (i.e., that the compatibility rules are implicit and that we can't specify what is compatible using arbitrary logic). Here's a summary of the [compatibility rules](https://stackoverflow.com/a/54949750/1143274). – Evgeni Sergeev Mar 21 '19 at 02:21
2

This:

Thread thread = new Thread(ScanDirectory)

still calls Thread constructor, and implicitly calls ThreadStart (or ParameterizedThreadStart, depending on signature of ScanDirectory method) constructor to make appropriate delegate instance.

Implicit call of delegate type's constructor is just a "syntactical sugar" of C# language, not the .NET runtime.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I want to know how to do this feature: "implicitly calls ThreadStart (or ParameterizedThreadStart)". Thanks – Leo Vo Sep 26 '12 at 06:25
  • @LuLu: what do you mean with "how to do this feature"? Just write the code you've posted here, and compiler will do this for you. – Dennis Sep 26 '12 at 06:27
2

Actually when you pass a void method, you're using the first constructor. Here's a quoted example from the documentation:

// To start a thread using an instance method for the thread  
// procedure, use the instance variable and method name when  
// you create the ParameterizedThreadStart delegate. C# infers  
// the appropriate delegate creation syntax: 
//    new ParameterizedThreadStart(w.DoMoreWork) 
//
Work w = new Work();
newThread = new Thread(w.DoMoreWork);
Amr
  • 1,935
  • 2
  • 19
  • 29
0

Here:

Thread thread = new Thread(new ThreadStart(ScanDirectory)); 

You are explicitly creating the ThreadStart delegate by passing the ScanDirectory void into the constructor

Here:

Thread thread = new Thread(ScanDirectory);  

The ThreadStart is being implicitly created because the ScanDirectory void has the same signature as the delegate.

In this case, the 2 are exactly the same.

Have a look at Jon Skeet's answer in this thread though for more detail

Community
  • 1
  • 1
Martin Booth
  • 8,485
  • 31
  • 31