2014-09-30 75 views
0

之間的區別是什麼主題是什麼

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

Thread t = new Thread (Go); 

去哪兒是一個方法

回答

0

沒有,我是。 Thread的構造函數採用類型爲delegateThreadStart「構造」。

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

ThreadStart定義爲:

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

由於每個方法可用於作爲代理可以直接傳遞到您的構造。通過明確書寫..

new Thread(new ThreadStart(Go)) 

..你簡單地把它包起來。

4

無之間的差異。他們是一樣的東西。

documentation美國這樣的:

Visual Basic和C#用戶可以創建一個線程時省略了ThreadStartParameterizedThreadStart委託構造函數。 [...]在C#中,只需指定線程過程的名稱即可。編譯器選擇正確的委託構造函數。

0

第二個是一個捷徑!基本上,做同樣的事情。但是,在對象threadstart內部有一組可以通知的參數。

6

是有差別的唯一情況是,如果Go是一個可以匹配多個Thread構造函數重載的方法 - 組 - 例如,因爲有兩個ThreadStartParameterizedThreadStart構造,下面的方法將使new Thread(Go)版本曖昧:

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

new Thread (new ThreadStart (Go))消歧,通過顯式地聲明瞭委託類型,但:比它們是相同的其他,對C#2或以上。注意:在C#2之前,較短的版本不是合法的語法。