[The BLINK tag in HTML] was a joke, okay? If we thought it would actually be used, we wouldn't have written it! --Mark Andreessen
Welcome to my blog and project site for Microsoft.NET development.

I've been a full time .NET developer for ten years, but I didn't start my professional life as a programmer ... more
Share/Print this page:

Partial Classes in .NET

The partial class concept in Visual Studio 2005

By steve on May 26, 2007.
Updated on January 22, 2012.
Viewed 31,324 times (31 times today).
Article AttributesArticle AttributesArticle TypesLanguagesMicrosoft StackTechnologies
Has DownloadsHas ImagesOverviewC#Visual StudioASP.NET

Downloads

Contents

Summary

Contents

If you're new to .NET 2.0, then you're new to the concept of partial classes. Partial classes simply allow a single class definition to span more than one file offering yet another useful way to organize your code.

First a basic example. Following is a simple class definition in C# which spans two files: Statistics_Public.cs and Statistics_Local.cs. This is just one possible way (and not a great one) that you might want to break up your code into separate files so that it is easier to manage.

If you come from the C or C++ world where you are used to include files and source files (mycode.h, mycode.c), then you could break up your code that way still perhaps naming your partial class files mycode.h.cs and mycode.cs.

Statistics_Public.cs partial class file

Contents
// filename: Statistics_Public.cs
namespace Cambia.PartialClasses
{
    public partial class Statistics
    {
        // basic sum
        public decimal Sum(decimal[] nums)
        {
            decimal sum = 0;
            foreach ( decimal d in nums )
                sum += d;
            return sum;
        }
        // sum only the even indexes: 0, 2, 4, 6 ...
        public decimal SumEvens( decimal[] nums )
        {
            decimal[] evens = GetEvens(nums);
            return Sum(evens);
        }
    }
}

Statistics_Local.cs partial class file

Contents
// filename: Statisics_Local.cs
namespace Cambia.PartialClasses
{
    public partial class Statistics
    {
        internal decimal[] GetEvens( decimal[] nums )
        {
            decimal[] outputNums = new decimal[(nums.Length+1)/2];
            for ( int i=0; i<nums.Length; i=i+2 )
                outputNums[i/2] = nums[i];
            return outputNums;
        }

    }
}

For this little example, breaking your code into separate files would be pretty silly. But there are cases where partial classes are useful.

Visual Studio uses partial classes to separate auto-generated code from user-generated code. For example, in the Windows Form application I created for this example my main form is named Form1 and the main code that I will write resides in Form1.cs, just as in previous version of Visual Studio.

The app itself is very simple and uses the Statistics class to add the even numbered lines in a list (where numbering starts with 0).

The main code file for the form looks like this. Note the use of the 'partial' keyword on the class definition.

Form1.cs

Contents
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace Cambia.PartialClasses
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click( object sender, EventArgs e )
        {
            string[] lines = textBox1.Lines;
            List<decimal> nums = new List<decimal>();
            try
            {
                foreach ( string line in lines )
                {
                    if ( line.Trim() == "" )
                        nums.Add(0);
                    else
                        nums.Add(Convert.ToDecimal(line));
                }
                Statistics stats = new Statistics();
                decimal sum = stats.SumEvens(nums.ToArray());
                textBox2.Text = sum.ToString();
            }
            catch ( Exception ex )
            {
                MessageBox.Show("Error parsing numbers.  "
                                + "Please enter 1 number per line.");
            }
        }
    }
}

But wait! Where are my buttons, labels and textboxes defined?

I know, I know. You're ahead of me already if you're even bothering to read my commentary, so I won't belabor it any longer.

Visual Studio creates a file called Form1.Designer.cs which holds the designer-related, auto-generated code. So in both Form1.cs and Form1.Designer.cs the Form1 class is declared as partial and the compiler joins them into one.

Note that Visual Studio does not have a partial designer class for ASP.NET. It is generated at runtime.

Form1.Designer.cs

Contents
namespace Cambia.PartialClasses
{
    partial class Form1
    {
        /// <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);
        }

        #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.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.textBox2 = new System.Windows.Forms.TextBox();
            this.label1 = new System.Windows.Forms.Label();
            this.SuspendLayout();
            // 
            // button1
            // 
            this.button1.Location = new System.Drawing.Point(156, 50);
            this.button1.Name = "button1";
            this.button1.Size = new System.Drawing.Size(99, 23);
            this.button1.TabIndex = 0;
            this.button1.Text = "Sum Evens >";
            this.button1.UseVisualStyleBackColor = true;
            this.button1.Click += new System.EventHandler(this.button1_Click);
            // 
            // textBox1
            // 
            this.textBox1.Location = new System.Drawing.Point(4, 50);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(146, 124);
            this.textBox1.TabIndex = 1;
            // 
            // textBox2
            // 
            this.textBox2.Location = new System.Drawing.Point(261, 50);
            this.textBox2.Multiline = true;
            this.textBox2.Name = "textBox2";
            this.textBox2.Size = new System.Drawing.Size(145, 124);
            this.textBox2.TabIndex = 2;
            // 
            // label1
            // 
            this.label1.Location = new System.Drawing.Point(1, 9);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(405, 38);
            this.label1.TabIndex = 3;
            this.label1.Text = "Enter one number per line in the left box."
            + "  Then click the button to sum every ot" 
            + "her one starting with the first.";
            // 
            // Form1
            // 
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(407, 178);
            this.Controls.Add(this.label1);
            this.Controls.Add(this.textBox2);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        #endregion

        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;
        private System.Windows.Forms.TextBox textBox2;
        private System.Windows.Forms.Label label1;
    }
}

Back to Top

User Comments (13)

Posted 2007 Sep 13 20:44 PM. reply
I have used to add more methods, fields to WSDL generated code so that we do not modify the class code coming from a Web service.
Be careful to add public properties as they can be serialized back to the WS if required it shall have the following attribute [XmlIgnore]

MASL
Replied 2009 May 29 01:01 AM by Paresh Patel. reply
hi sir,
i want to know what is the difference between Partial Class and Class.... please send me simple example as we can understand. My Email Address is paresh.seek.dba83@gmail.com
Posted 2007 Sep 17 00:19 AM. reply
Thanks for this information, i am using dotnet for a 1+ yrs and i still was unawared about why they declared Form1 as partial class.

Thank You.

Anup
Posted 2007 Oct 15 04:33 AM. reply
Thank you, It is really a good example which you have explained clearly about how to use partial class in .NET

Rajakiran
Posted 2007 Oct 25 08:52 AM. reply
Thanks.Gud Example to Provided

Nishchal
Posted 2007 Nov 15 00:51 AM. reply
Include information about how to include namesapce to partial class (code behind) in asp.net?

Vijay
Posted 2010 Jan 04 02:13 AM. reply
listed views all controlls do the string.

sarita sharma
Posted 2010 Jan 16 04:07 AM. reply
Good One

Rohit Kharbanda
Posted 2010 Sep 08 06:56 AM. reply
thanks for the detailed

Ratnesh
Posted 2011 May 18 01:33 AM. reply
Hi sir,
i want to know what is the difference between public partial & private partial.... please send me simple example & point wise answers as we can understand. My Email Address is bhuteyogesh81@gmail.com

yogesh
Posted 2011 Jun 20 00:54 AM. reply
hi,
sir i am not getting properly about partial classes .please send me clearly

arun bhardwaj
Posted 2011 Nov 10 00:52 AM. reply
Very Good Information with our day to day activity....

Nayana
Posted 2011 Dec 04 23:30 PM. reply
how to write code passward matching........... plz reply..........

sandeep singh
Post Your Comment
  You may post without logging in or login here.
Display Name: Required.
Email: Required. Will not be shown. Used for identicon.
Comment:
Allowed tags: <quote></quote>, <code></code>, <b></b>, <i></i>, <u></u>, <red></red>
 
   Please type text as shown in the image at left.