0

Possible Duplicate:
When and why should I implement IComponent, IContainer, and ISite?

Every time I create new control, Visual Studio adds the next field

private System.ComponentModel.IContainer components = null;

But never uses it. What for?

Community
  • 1
  • 1
Nelson Tatius
  • 7,693
  • 8
  • 47
  • 70

2 Answers2

0

You have container of your controls

  1. A container removes the need for a component to locate its dependencies or manage their lifetimes.

  2. A container allows swapping of implemented dependencies without affecting the component.

  3. A container facilitates testability by allowing dependencies to be mocked.

  4. A container increases maintainability by allowing new components to be easily added to the system.

Lesmana
  • 25,663
  • 9
  • 82
  • 87
Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51
0

A user control is simply a what to create your own "control". If that control consists of other child controls, then those controls should be disposed of when your control is disposed of (common courtesy for Disposable objects, etc.).

User control is a parent control in this respect; so, by default the class creates a container for these child controls. You're able to "design" the user control in the Designer and drag-and-drop controls from the toolbox onto the design surface--which knows about the components field and makes sure those child controls are added to the collection.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98