构造函数可以使用 this 关键字在同一个对象上调用另一个构造函数。
构造函数可以使用 base 关键字来调用基类构造函数。
文档清楚地描述了如何单独使用它们。但是没有提到是否可以在同一个构造函数中同时使用 this(...) 和 base() 。
例如(代码抛出错误):
class A {
private string PropA {get; set;}
public A() {}
public A(string propA) this.PropA = propA;
}
class B : A {
private string PropB_1 {get; set;}
private string PropB_2 {get; set;}
public B(string propB_1) => this.PropB_1 = PropB_1;
public B(string propB_1, string propB_2, string propA) : this(propB_1), base(A) // ошибка
{
this.PropB_2 = propB_2;
}
}
告诉我,c# 是否支持同时调用 this(...) 和 base(...) 的能力?如果是,请指出语法。
您不能同时使用
this和base-。但是,您可以调用
thiswhich 将调用所需base的,例如: