您可以使用它来使用您想要的值快速实例化类的属性:
Form myform = new Form
{
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = "Hello world!",
StartPosition = FormStartPosition.CenterScreen
};
但是下面的语法无效:
class MyForm: Form
{
public MyForm(string caption) : base() {
Width = 500,
Height = 150,
FormBorderStyle = FormBorderStyle.FixedDialog,
Text = caption,
StartPosition = FormStartPosition.CenterScreen}
{
}
}
所以我想知道是否有办法做到这一点,或者我是否只需要做旧学校,就像这样:
class MyForm: Form
{
public MyForm(string caption) : base()
{
Width = 500;
Height = 150;
FormBorderStyle = FormBorderStyle.FixedDialog;
Text = caption;
StartPosition = FormStartPosition.CenterScreen;
}
}
我承认这并不是一个可怕的差异,但我仍然为了知识而感兴趣。