MohamedG's Log

TW9oYW1lZCBFbC1HZWlzaA==

Declare and set properties all at once

I used to use this technique in ActionScript, and I really like to use it C#:


TextBox textBox = new TextBox() { Text = “Hello!”, Enabled = false };

TextBox
textBox = new TextBox { Text = “Hello!”, Enabled = false }; // Thanks, kfarmer

Point
point = new Point(size) { X = 5 };


The only constructor that the TextBox class has is parameterless, but you can declare it and set its properties as shown in the examples. As kfarmer noted in his comment below, the parentheses are optional in case of a parameterless constructor.