So the question is, if I am instantiating a control for use on a form (or any object really) at what point is the object and it's properties/methods considered loaded into memory?

The reason why I ask is because evidently when I instantiate my custom controls, but before adding them to the form, they are not "loaded" and the Load event is not triggered until I explicitly add the control to the form.

For example, the code MyControl myControl = new MyControl(); does nothing, but as soon as MyForm.Controls.Add(myControl); is called, the load event for MyControl is triggered. Is this odd to anyone else? If the load event is not triggered at instantiation wouldn't the event name be kind of deceiving? I mean, an instantiated object, by definition is loaded, isn't it?

It would really depend on the type of object.
Have you run across the term "lazy loading", yet?
Another example

Primitives are different.
Static elements are different.
Objects created by LINQ are different (unless you force them to a type).

Edited 4 Years Ago by thines01: n/a

Comments
That about sums it up I guess.

If you look at the documentation (assuming UserControl is your base class):

The Load event occurs when the handle for the UserControl is created. In some circumstances, this can cause the Load event to occur more than one time.

In WinForms, controls are essentially wrappers for lower level classes. For example, you instantiate a TextBox in .NET. This reserves some memory for that class. At some point, before the control is shown, it creates a handle for a control (like you would do if you used the Windows API). This handle is used by low-level functions to display and query the control. Because programmers are not inherently efficient, the .NET framework is designed to make up for any lacking in efficiency. There is no need to create that handle right away. A programmer may instantiate a control class, but never use it, therefore it waits. It may also release the handle when a control is removed from a Form, and recreate it later when it is used again. If you want something to execute when the class is instantiated, put it in the constructor.

Essentially, this is lazy loading like thines01 said.

Edited 4 Years Ago by nmaillet: n/a

Thanks for the replies!

That explains a lot! I had never heard of lazy loading! Thanks for the info!

This question has already been answered. Start a new discussion instead.