简体   繁体   English

为什么类Program的默认构造函数从不执行?

[英]Why the default constructor of class Program is Never executed?

namespace TestApp
{
  class Program
  {
    public Program()
    {
      var breakpoint1 = 0;
    }

    static void Main(string[] arguments)
    {
      var breakpoint2 = 0;
    }
  }
}
  1. Why breakpoint 1 is never hit , but it hits breakpoint 2 always? 为什么breakpoint 1永远不会被击中,但它始终击中breakpoint 2
  2. And is there a way to execute the default constructor before entering Main() ? 有没有办法在进入Main()之前执行默认构造函数?

The Main method is executed without an instance of the Program class, which is possible because it is a static method. Main方法在没有Program类实例的情况下执行,这是可能的,因为它是一个静态方法。 Static methods are methods that can be called without the need to construct/instantiate an object from the class. 静态方法是可以调用的方法,无需从类中构造/实例化对象。 They can be called directly on the Class itself like this: 它们可以直接在类本身上调用,如下所示:

Program.Main(new string[0]); 

// executes the Main static method on Program class 
// with empty string array as argument

The constructor is not a static method, to hit that breakpoint you need to instantiate the Program class, like this: 构造函数不是一个静态方法,要点击你需要实例化Program类的断点,如下所示:

static void Main(string[] arguments)
{
  var breakpoint2 = 0;
  new Program(); // breakpoint1 will be hit
}

Alternatively you can make the constructor static , though admittedly it is not really that useful from a testability standpoint and also implies that you're going to have static variables (that are globally available): 或者你可以使构造函数变为静态 ,但是从可测试性的角度来看它确实不是那么有用,并且暗示你将拥有静态变量(全局可用):

static Program() {
    var breakpoint1 = 0; 
    // breakpoint will be hit without an instance of the Program class
}

You can read more about static methods here . 您可以在此处阅读有关静态方法的更多信

You are not instantiating the class. 你没有实例化这个类。 You are running a static Main() method. 您正在运行static Main()方法。 The run time will load the class and invoke the Main() method .It doesn't need an instance of the class to invoke the Main() method. 运行时将加载类并调用Main()方法。它不需要类的实例来调用Main()方法。 Constructor will run when you construct(instantiate) an object. 构造函数将在构造(实例化)对象时运行。

Your method is static: it will run without an instance of the class existing. 您的方法是静态的:它将在没有现有类的实例的情况下运行。 Your constructor is not: it is only executed when you create an instance of the class (that is, when you write new Program() ). 您的构造函数不是:它只在您创建类的实例时(即,当您编写new Program() )执行。

In order to hit your breakpoint, most likely you want to change your constructor to be static Program() instead. 为了达到您的断点,您很可能想要将构造函数更改为static Program()

The entry point to your program is equivalent to calling TestApp.Program.Main(args) . 程序的入口点等同于调用TestApp.Program.Main(args) The Program class doesn't get instantiated. Program类没有实例化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么默认构造函数在静态构造函数之前执行? - Why the default constructor executed before the static constructor? 默认构造函数从未调用 - Default Constructor never called 为什么MessageBox类在C#中没有默认构造函数? - Why doesn't the MessageBox class have a default constructor in C#? 为什么类需要默认构造函数,但结构却不需要? - Why class required default constructor but structure dont required? 为什么不执行BaseController的重载构造函数? - Why is BaseController's overloaded constructor not being executed? Unity 不使用类的默认构造函数 - Unity not using the default constructor of the class 为什么具有默认值枚举参数的泛型类的构造函数不能调用该类的受保护方法? - Why is the constructor of a generic class with a default-valued enum parameter not able to call protected methods of that class? 构造函数混淆 - '从未分配给,并且将始终具有其默认值' - Constructor Confusion - 'never assigned to, and will always have its default value' 构造函数DI - Field永远不会分配给,并且始终具有其默认值 - Constructor DI - Field is never assigned to, and will always have its default value 为什么这个短程序永远不会完成? - Why does this short program never complete?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM