Disposing Controls on Winforms
c#, winforms
Solution
The private field `components` is used to track disposable components on your form. Try dragging in a `Timer` component, and you should see something like this in the designer generated code:
this.components = new System.ComponentModel.Container();
this.timer1 = new System.Windows.Forms.Timer(this.components);
The pattern displayed in the `Dispose(bool)` method is usually referred to as the disposable pattern. Basically, the pattern ensures that all of the tracked components will be disposed, even if you never call the `Dispose` method explicitly, in which case the base class of your form will call the `Dispose` method in its finalizer (during garbage collection).
Problem
Why does Visual Studio add this code to the Class.Designer.cs partial class. Can anyone tell me when this components variable is going to get some value? What's the pattern to follow here? ``` 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); } ```