我有两个构造函数,它们将值提供给只读字段.
public class Sample
{
public Sample(string theIntAsString)
{
int i = int.Parse(theIntAsString);
_intField = i;
}
public Sample(int theInt) => _intField = theInt;
public int IntProperty => _intField;
private readonly int _intField;
}
Run Code Online (Sandbox Code Playgroud)
一个构造函数直接接收值,另一个构造函数进行一些计算并获取值,然后设置字段.
现在这里是抓住:
有任何想法吗?
在C#中,当你这样做时
Class(Type param1, Type param2) : base(param1)
Run Code Online (Sandbox Code Playgroud)
是先执行的类的构造函数,然后调用超类构造函数还是先调用基础构造函数?
我只是在C#中读取继承,其中我遇到了构造函数,并且编写了构造函数按派生顺序执行.它是什么意思?将首先调用该基类构造函数或派生类.