实现某些东西的构造函数

时间:2013-10-02 18:43:31

标签: c# constructor

我有下面的代码,我正在尝试理解它的作用:

readonly Ido _do;

public Main(): this(new doX()) { }

public Main(Ido do) {
    _do = do;
}

这在第一个构造函数中做了什么?

this(new doX())

以及应用程序首先以

开头的原因
Main(Ido do)

然后调用

this(new doX())

虽然我认为它应该是其他方式

3 个答案:

答案 0 :(得分:0)

this(new dox)正在调用构造函数,该构造函数是您的类的参数化构造函数。您可以使用 this 关键字在同一对象中调用另一个构造函数。

答案 1 :(得分:0)

调用该类的其他构造函数。

: base(...) // Call a base class contructor
: this(...) // Call another constructor in the same class

答案 2 :(得分:0)

通过调用new Main()调用默认构造函数(即没有参数)将首先执行调用第二个构造函数的this(new doX())

尝试逐步调试调试器中的代码,它应该变得清晰。

相关问题