Is a way to set the properties of an object in a block using C#, similar to how you write an object initializer?
For example:
Button x = new Button(){
Text = "Button",
BackColor = Color.White
};
Is there a syntax similar to this that can be properties after the object has been created?
For example:
Button x = new Button();
x{
Text = "Button",
BackColor = Color.White
};
This form
Button x = new Button(){
Text = "Button",
BackColor = Color.White
};
Is part of the syntax for constructors and constructors only. You can't use the same syntax on the next line. You can leave out the ()
, though, and use a var
for the variable type, to give you the more compact;
var x = new Button{
Text = "Button",
BackColor = Color.White
};
After construction, the only way to update it is through normal assignment operations;
x.Text = "Button";