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.
It’s important to separate two things here:
ThreadconstructorThreadconstructorYou’re really interested in the latter here – the difference between:
and
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: