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:
Thread
constructorThread
constructorYou'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)
{
}
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With