1.7. Hide Designer Code with Partial TypesIn previous versions of C# the entire definition for a class had to be in a single file. Now, using the partial keyword, you can split your class across more than one file. This provides two significant advantages:
Note: Using the partial keyword, you can split your class across more than one file. 1.7.1. How do I do that?The easiest way to see partial types at work is to examine the previous example (AnonymousMethods). Examine the declaration of the class in Form1.cs: partial class Form1 : Form { public Form1( ) { InitializeComponent( ); this.button1.Click += delegate { label1.Text = "Goodbye"; }; } // private void button1_Click(object sender, EventArgs e) // { // label1.Text = "Goodbye"; // } } The partial keyword indicates that the code in this file does not necessarily represent the complete definition of this class. In fact, you saw earlier that the Visual Studio 2005 designer generated a second file, Form1.Designer.cs, which contains the rest of the definition: namespace AnonymousMethods { 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 /// Designer-generated initialization code ... #endregion private System.Windows.Forms.Label label1; private System.Windows.Forms.Button button1; } } Together, these two files completely define the Form1 class, but you are spared dealing with the designer-generated code unless you need to work with it. This makes for simpler and cleaner development. There is some "fine print" you need to be aware of in regard to using partial classes:
1.7.2. What about . . ....using partial classes in my own projects? Microsoft suggests that partial classes can allow developers to work on different aspects of a class independently. It is still too early to see what best practices will emerge; I'm inclined to think that any class that is big enough to be divided in this way is big enough to be split into two (or more) classes. For now, the primary use of partial classes is to hide the cruft created by the designer. Tip: Robert MacNeil reports in the PBS documentary "Do You Speak American?" that cruft is a neologism invented by surfers. However, the Online Computing Dictionary (http://www.instantweb.com/D/dictionary/index.html) reports that "This term is one of the oldest in the jargon and no one is sure of its etymology." In any case, the Online Computing Dictionary defines cruft as "an unpleasant substance...excess; superfluous junk" and "the results of shoddy construction." 1.7.3. Where can I learn more?Developer.com provides a good article on partial types. Visit http://www.developer.com/net/net/article.php/2232061 for more information. |