Kuttan Sujith
Kuttan Sujith

Reputation: 7979

Thread What is the difference between

What is the difference between

Thread t = new Thread (new ThreadStart (Go));

and

Thread t = new Thread (Go);

where Go is a method

Upvotes: 2

Views: 108

Answers (4)

Marc Gravell
Marc Gravell

Reputation: 1062780

The only time there is a difference is if Go is a method-group that could match multiple Thread constructor overloads - for example, because there is a constructor for both ThreadStart and ParameterizedThreadStart, the following methods would make the new Thread(Go) version ambiguous:

static void Go() { }
static void Go(object val) { }

The new Thread (new ThreadStart (Go)) disambiguates that by explicitly declaring the delegate type, but: other than that they are identical, on C# 2 or above. Note: prior to C# 2, the shorter version was not legal syntax.

Upvotes: 7

Juri
Juri

Reputation: 32910

None, I gess. The constructor of Thread takes a ThreadStart "construct" which is of type delegate.

// constructor of Thread
public Thread(ThreadStart start);

ThreadStart is defined as:

namespace System.Threading
{
    // Summary:
    //     Represents the method that executes on a System.Threading.Thread.
    [ComVisible(true)]
    public delegate void ThreadStart();
}

Since every method can be used as a delegate you can directly pass yours into the constructor. By explicitly writing..

new Thread(new ThreadStart(Go))

..you simply wrap it again.

Upvotes: 0

The second is one is a shortcut! Basically, does the same thing. But, inside of the object threadstart there are a set of parameters that you can inform.

Upvotes: 0

RePierre
RePierre

Reputation: 9566

None. They are the same thing.

The documentation states this:

Visual Basic and C# users can omit the ThreadStart or ParameterizedThreadStart delegate constructor when creating a thread. [...] In C#, simply specify the name of the thread procedure. The compiler selects the correct delegate constructor.

Upvotes: 4

Related Questions