46

When you create a new form in Visual Studio, the designer generates the following code in the .Designer.cs file:

  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.IContainer components = null;

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
  protected override void Dispose(bool disposing)
  {
     if (disposing && (components != null))
     {
        components.Dispose();
     }
     base.Dispose(disposing);
  }

What is the purpose of the components variable? My theory is that I should use it for any IDisposable classes my form owns which I create outside of Designer (since Dispose is already being implemented by the Designer).

So, for example, if my form owns a font, I could make sure it gets disposed by adding it to components like this:

  public partial class Form1 : Form
  {
      Font coolFont;

      public Form1()
      {
          InitializeComponent();
          this.coolFont = new Font("Comic Sans", 12);
          components.Add(this.coolFont);
      }
  }

Is that what it's for? I haven't been able to find any documentation or information about this.

2 Answers 2

28

When you add non-UI components to the form (such as a Timer component), components will be the parent of those compoments. The code in the designer file makes sure that these components are disposed of when the form is disposed. If you have not added any such components to the form in design time, components will be null.

Since components is designer generated, and will be null if you have no non-UI compoments on the form (in design time), I would personally opt for managing those components in some other way, disposing them on form close or something like that.

3
  • 1
    I am trying to understand this more deeply. I have tried adding components at design time and the components instance variable is still always null. If the components instance variable were set to something other than null, where could I find the code that is setting it? Dec 6, 2012 at 1:41
  • 2
    I got it to set the components instance variable in the designer generated code by adding a SerialPort component from the designer. So I understand this now. I wish it would just not generate the components and Dispose code when it is not being used, that would have avoided the confusion and resulted in less code, but whatchucando Dec 6, 2012 at 1:58
  • Should it be another question, on how to get rid of the compiler warning CS0414 that the programmer is besieged with when components is never used?
    – PandaWood
    Jun 27, 2016 at 7:59
28

The components variable is the equivalent of the form's Controls variable. Which keeps track of all the controls put on a form. So that a form can automatically dispose all the controls when it is closed, a very important clean-up duty.

The form class has no equivalent member that keeps track of all the Components that were dropped on it at design time so the designer takes care of it automatically.

Note that moving the Dispose() method from the Designer.cs file to the main form source code file is quite acceptable. I strongly recommend you do so, no reason to make the Form class 'special' in any way, it is just a managed class like any other. Add Dispose() calls to dispose members as needed before the base.Dispose call.

1
  • 15
    Good to know it's acceptable to move Dispose out of the Designer.cs file.
    – bentsai
    Feb 21, 2011 at 19:02

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.