是否有一个方便的过程可以将下面构造函数中的属性“Caller”的值分配给单行?
public partial class SecurityPrompt : Form {
Form1 Caller { get; set; }
public SecurityPrompt(Form1 _caller) {
Caller = _caller;
InitializeComponent();
}
}
最佳答案
有object initializer syntax ,但这会创建构造函数,然后分配公共(public)属性。如果你想使用它,你必须在这里重构你的构造函数。
使用对象初始值设定项:
The compiler processes object initializers by first accessing the default instance constructor, and then by processing the member initializations.
如果您确实使用此属性,则在创建构造函数之前不会设置您的属性。因此,如果您在构造函数中调用函数并期望您的属性在那里,那么它不会。请记住这一点。
关于c# - 方便初始化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12498312/