Winforms designed code and C# partial classes

In Whidbey, the WinForms designer takes advantage of a new C# language feature called “partial classes”. This allows them to pull out the designer generated code into a separate file. It has several advantages:

· Users are less likely to muck with it, since it’s in a different file. When users start to edit designer generated code, things break down pretty quickly. It’s pretty common for the designer to eat code it doesn’t understand, causing you to lose your work.

· The code generator can use no ‘using’ directives, instead having only fully qualified names (FQN). It’s a good idea for code generators to use FQNs, because it makes them resilient to unforeseen changes in the compilation environment (like someone adding a class named “System”

· It reduces clutter in the user file, letting you focus on your work.

To show what this looks like, I created a simple C# Windows Application, and added an OK button. Here is the result:

------- program.cs -------

#region Using directives

using System;

using System.Collections.Generic;

using System.Windows.Forms;

#endregion

namespace WindowsApplication1

{

    static class Program

    {

        /// <summary>

    /// The main entry point for the application.

        /// </summary>

        [STAThread]

        static void Main()

        {

            Application.EnableVisualStyles();

            Application.EnableRTLMirroring();

            Application.Run(new Form1());

        }

    }

}

------- Form1.cs -------

#region Using directives

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Windows.Forms;

#endregion

namespace WindowsApplication1

{

    partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

    }

}

------- Form1.Designer.cs -------

namespace WindowsApplication1

{

    partial class Form1

    {

        /// <summary>

   /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        protected override void Dispose(bool disposing)

        {

            if (disposing && (components != null))

            {

                components.Dispose();

            }

            base.Dispose(disposing);

        }

        #region Windows Form Designer generated code

      /// <summary>

        /// Required method for Designer support - do not modify

        /// the contents of this method with the code editor.

        /// </summary>

        private void InitializeComponent()

        {

            this.buttonOK = new System.Windows.Forms.Button();

            this.SuspendLayout();

//

// buttonOK

//

            this.buttonOK.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right)));

            this.buttonOK.Location = new System.Drawing.Point(205, 238);

            this.buttonOK.Name = "buttonOK";

            this.buttonOK.TabIndex = 0;

            this.buttonOK.Text = "OK";

//

// Form1

//

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);

            this.ClientSize = new System.Drawing.Size(292, 273);

            this.Controls.Add(this.buttonOK);

            this.Name = "Form1";

            this.Text = "Form1";

            this.ResumeLayout(false);

        }

        #endregion

        private System.Windows.Forms.Button buttonOK;

    }

}

--------------------------

What do you think of the new default template? It’s an improvement, no?