Main
方法執行時沒有Program
類的實例,這是可能的,因爲它是一種靜態方法。靜態方法是可以在不需要從類構造/實例化對象的情況下調用的方法。他們可以直接在類本身被稱爲是這樣的:
Program.Main(new string[0]);
// executes the Main static method on Program class
// with empty string array as argument
構造函數是不是一個靜態的方法,來打的斷點,你需要實例化Program
類,像這樣:
static void Main(string[] arguments)
{
var breakpoint2 = 0;
new Program(); // breakpoint1 will be hit
}
或者你可以make the constructor static,但無可否認它不是really that useful from a testability standpoint,也意味着你將有靜態變量(即在全球範圍內提供):
static Program() {
var breakpoint1 = 0;
// breakpoint will be hit without an instance of the Program class
}
您可以閱讀更多關於static methods here。
您應該瞭解靜態和非靜態方法的區別。 –