Haider Ali
Haider Ali

Reputation: 2795

why initialized components before SuspendLayout in c# winforms

i am designing a windows form in c# .Net fw-3.5,

i have check out the generated code by c# in designer.cs file

        this.label1 = new System.Windows.Forms.Label();
        this.button1 = new System.Windows.Forms.Button();
        this.textBox1 = new System.Windows.Forms.TextBox();

        this.SuspendLayout();

and after SuspendLayout command all components properties are assigning.

my question: is here any special reason to initialize object before SuspendLayout ?

Upvotes: 0

Views: 466

Answers (2)

Ed Swangren
Ed Swangren

Reputation: 124642

Because there is no need to do it beforehand. Object initialization is irrelevant to the SuspendLayout call, so why do it before? Even autogenerated code can be laid out logically. If I were writing that code by hand I would do the same thing for the same reason I declare variables as close as possible to where they are first used (of course, that example would make the generation process a bit more complicated, so they just initialize them at the start).

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500515

It's really not going to make any difference either way - it's not like the components have been added to the form yet, so it won't be placing them, then suspending layout, then changing everything.

I suspect it just makes it easier for the code to be generated if all the variables are assigned and then used, rather than having to work out whether there were any dependencies between the various objects.

Upvotes: 2

Related Questions