• 0

[.NET] Constructors and InitializeComponent()


Question

Maybe I'm being overly meticulous, but I can't help but notice that VS.NET puts initializations inside the InitializeComponent method instead of the constructor. To keep it short, why is it designed this way? Why not put the initilizations in the constructor, where they traditionally go? Heck, when I declare and initialize (at the same time) a private field in C#, the compiler moves the initalization into the constructor anyway... :huh: Why the need for an additional method that only gets called once during the lifespan of the object, during it's birth?

3 answers to this question

Recommended Posts

  • 0

Yeah i think it's just the neatness, think about it, if you chuck all that initialize component code into the constructor it would look messy, especially if you have any other custom calls in the constructor, seperating component initialization and custom initializations would be a little difficult. And after doing a simple test, the InitializeComponent method seems to be designed for the IDE, so that when you use the form designer, it can easily append code into that particular method/procedure, notice if you change the name of the method, from InitializeComponent to something like InitComp, and going back tot he form designer and adding a new control on the form, the IDE will automatically regenerate a new method with the name InitializeComponent.

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.