Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does new Thread() accept a method name, even though none of the constructor overloads seem to allow this? [duplicate]

Tags:

c#

.net

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.

like image 496
Leo Vo Avatar asked Sep 26 '12 06:09

Leo Vo


3 Answers

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)
{
}
like image 165
Jon Skeet Avatar answered Nov 11 '22 22:11

Jon Skeet


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.

like image 40
Dennis Avatar answered Nov 11 '22 22:11

Dennis


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);
like image 2
Amr Avatar answered Nov 11 '22 21:11

Amr