Hello! This question is about optimality.

Which way of the two, when programmatically creating a control in WinForms, is it better to set the coordinates?

1) element.Location = new Point(x, y);
2) element.Location.X = x; element.Location.Y = y; element.Location.X = x; element.Location.Y = y;

Is it advisable to initialize an instance of Point (although it takes up less space and looks prettier)?

  • Please accept another answer, my answer was wrong. - VladD

2 answers 2

2) element.Location.X = x; element.Location.Y = y;

This option is not working. In the Location you can write the entire Point , and access to Location.X and Location.Y read-only, since Location property and not a variable or field, which the studio warns about before compiling.

The reason is quite simple - the structures in .NET refer to types of values ​​(value types), therefore the property when accessing it provides a copy of the value of Point and there is no point in making changes to it.

If we start from beauty, then point creation is better (since we use object-oriented language =) In this case, in hundreds of lines of code this will be clearer and more convenient to look at.