Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InitializeComponent in both constructors, or in one with constructor inheritance?

Is there any practical difference in terms of effects on the component model between:

class MyComponent : Component {
    public MyComponent() {
        InitializeComponent();
    }

    public MyComponent(IContainer container) {
        container.Add(this);
        InitializeComponent();
    }
}

and:

class MyComponent : Component {
    public MyComponent() {
        InitializeComponent();
    }

    public MyComponent(IContainer container) : this() {
        container.Add(this);
    }
}

and if not, why did Microsoft pick the first method for their designer-generated code?

Edit: What I mean is, will there be any side effects towards the change of order between initializing the component and adding it to the container?

like image 871
Alex J Avatar asked Nov 15 '25 13:11

Alex J


1 Answers

Order of execution differs. In

public MyComponent(IContainer container) {
    container.Add(this);
    InitializeComponent();
}

InitializeComponent() is executed after container.Add(), whereas here

public MyComponent(IContainer container) : this() {
    container.Add(this);
}

container.Add() is executed after InitializeComponent()

like image 63
Anton Gogolev Avatar answered Nov 17 '25 08:11

Anton Gogolev