According to C # documentation :

A constructor can call another constructor in the same object using the this keyword.

The constructor can use the base keyword to invoke the constructor of the base class.

The documentation clearly describes how to use them separately. But nothing is said about whether this (...) and base () can be used simultaneously in the same constructor.

For example (the code causes an error):

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; } } 

Tell me, does c # support calling this (...) and base (...) at the same time? If yes, please specify the syntax.

  • one
    not at the same time, but you can call this which will call base - Grundy
  • @Grundy can answer? - tym32167
  • one
    Any this as a result explicitly or implicitly calls the base , so you cannot simultaneously write this and that, just call the correct this , if it is not there - write it (you can write it private if you don't want it to be accessible from the outside) - Andrey NOP

1 answer 1

You cannot use this and base same time.

However, you can call this , which will call the desired base , for example:

 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, string propA) : base(propA) => this.PropB_1 = propB_1; public B(string propB_1, string propB_2, string propA) : this(propB_1, propA) // нет ошибки { this.PropB_2 = propB_2; } }