我有一个关于链接构造函数的问题我在 StackOverflow 和一些 c# 文章上阅读了一些问题,但我无法完全理解该主题。所以我有一个由 DerivedClass 继承的 BaseClass。在 DerivedClass 中,我没有参数构造函数,但它使用 base() 调用基本构造函数,并且它还传递了一个值。这是构造函数中使用的 base 关键字的主要目的,用于将值从派生类传递给继承的类,还是更多。而且在派生类中,我们还有第二个构造函数,它接受 1 个参数及其用法:this()。我不明白为什么当我删除时: this() 从这个构造函数“VS”告诉我“没有给定的参数对应于 BaseClass.BaseClass(int) 所需的形式参数“i”?为什么我不能
public class BaseClass
{
protected int _Num;
public BaseClass(int i)
{
_Num = i;
}
public int Num { get => this._Num ; set => _Num = value; }
}
public class DerivedClassA : BaseClass
{
private string _Name;
private int _AnotherValue;
public string Name { get => this._Name ; set => this._Name = value; }
public int AnotherValue { get => this._AnotherValue; set => this._AnotherValue = value; }
public DerivedClassA() : base(123)
{
_Name = "testing";
}
public DerivedClassA(int param2) : this() <-- Why i can't compile the program without the this() keyword here ?
{
AnotherValue = param2;
}
}
public class Program
{
public static void Main(string[] args)
{
DerivedClassA objA = new DerivedClassA(5);
}
}