是使用C#设置块中对象属性的一种方法,类似于您如何编写对象初始值设定项?C#:在一个块中设置对象的属性
例如:
Button x = new Button(){
Text = "Button",
BackColor = Color.White
};
是否有与此类似,可以是属性的对象被创建后语法?
例如:
Button x = new Button();
x{
Text = "Button",
BackColor = Color.White
};
可能的复制[With块等效于C#?(http://stackoverflow.com/questions/481725/with-block-equivalent-in-c) – sstan
这被称为[对象初始化]( https://msdn.microsoft.com/en-us/library/bb384062.aspx),只能在新建对象时使用。你的第二段代码根本无效C#。 –
谢谢 - Pieter Witvoet.You给了我我想要的答案 –