C# 如何呼叫另一个ctor?
例如,在我的类Foo中有两个ctor方法,如何在另一个ctor中调用无参数的ctorC# 如何呼叫另一个ctor?,c#,constructor,C#,Constructor,例如,在我的类Foo中有两个ctor方法,如何在另一个ctor中调用无参数的ctor class Foo { public Foo() { // initialized this class } public Foo(int a, int b) { // initialized by Foo(), how do I call Foo() here ? .... // other initializing here } } 添加
class Foo {
public Foo() {
// initialized this class
}
public Foo(int a, int b) {
// initialized by Foo(), how do I call Foo() here ?
.... // other initializing here
}
}
添加:此选项位于参数列表和开口大括号之间:
class Foo {
public Foo() {
}
public Foo(int a, int b) : this() {
}
}
将其放入初始值设定项列表中,如下所示:
public Foo(int a, int b) : this() {
// initialized by Foo(), how do I call Foo() here ?
.... // other initializing here
}
@Toro,你还应该注意,你可以用base[param list]@Slugster以同样的方式调用基构造函数,谢谢,我会注意到。在C中,基类初始值设定项列表的正确术语是什么?我现在不记得了;我不知道它有没有什么特别的名字。