[英]Thread What is the difference between
之間有什么區別
Thread t = new Thread (new ThreadStart (Go));
和
Thread t = new Thread (Go);
Go是一種方法
唯一的區別是,如果Go
是可以匹配多個Thread
構造函數重載的方法組-例如,由於同時存在ThreadStart
和ParameterizedThreadStart
的構造函數,因此以下方法會使new Thread(Go)
版本含糊不清:
static void Go() { }
static void Go(object val) { }
new Thread (new ThreadStart (Go))
通過顯式聲明委托類型來消除歧義,但是:在C#2或更高版本上,除了它們是相同的以外,它們是相同的。 注意:在C#2之前,較短的版本不是合法語法。
沒有。 他們是一樣的東西。
該文檔指出:
創建線程時,Visual Basic和C#用戶可以省略
ThreadStart
或ParameterizedThreadStart
委托構造函數。 [...]在C#中,只需指定線程過程的名稱。 編譯器選擇正確的委托構造函數。
第二個是一個捷徑! 基本上,做同樣的事情。 但是,在對象threadstart內部有一組可以通知的參數。
沒有,我很煩。 Thread
的構造函數采用ThreadStart
“ construct”類型的delegate
。
// 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))
..您只需再次包裝即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.