Metoda Main
jest wykonywana bez instancji klasy Program
, co jest możliwe, ponieważ jest to metoda statyczna. Metody statyczne to metody, które można wywoływać bez potrzeby konstruowania/tworzenia instancji obiektu z klasy. Mogą być wywoływane bezpośrednio na samej klasy jak poniżej:
Program.Main(new string[0]);
// executes the Main static method on Program class
// with empty string array as argument
Konstruktor nie jest metoda statyczna, uderzy to przerwania trzeba instancję klasy Program
, tak:
static void Main(string[] arguments)
{
var breakpoint2 = 0;
new Program(); // breakpoint1 will be hit
}
Alternatywnie można make the constructor static, choć trzeba przyznać, że nie jest really that useful from a testability standpoint a także sugeruje, że masz zamiar mieć zmienne statyczne (które są dostępne globalnie):
static Program() {
var breakpoint1 = 0;
// breakpoint will be hit without an instance of the Program class
}
Możesz przeczytać więcej o static methods here.
Powinieneś przeczytać o różnicy metod statycznych i niestatycznych. –