views:

560

answers:

2

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?

+1  A: 

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()

Anton Gogolev
But does this make any difference in terms of effects on the component model? I probably should have been more clear about that.
Ilia Jerebtsov
A: 

I believe the order of invocation will be different between the two. I believe in the second one, this() is called first, then the contents of the method. This means InitializeComponent() will be called before container.Add.

While that may not make a huge difference, it is a difference nonetheless.

NilObject