SIGN UP MEMBER LOGIN:    
ARTICLE

4 Real Time Use of Partial Classes and Partial Methods.

Posted by Shivprasad Articles | .NET 4.5 January 12, 2012
In this article we will explain about use of partial classes and partial methods for 4 real time.
Reader Level:

Introduction




I have also created a video on partial classes and shown the real time use of the same.

Fundamentals of partial classes

A partial class allows a single class to be divided in to two separate physical files. During compile time these files get compiled in to single class.  For instance you can see in the below figure we have the customer class divided in to two different files "customer1.cs" and "customer2.cs".

During compilation these files gets compiled in to single class internally. So when you create an object of the customer class you will be able to see methods lying in both the physical files. For instance you can see "Add" method belongs to "customer1.cs" and "Delete" method belongs to "customer2.cs" , but when the customer object is created we can see both "Add" and "Delete" methods.


2.jpg

Fundamentals of partial methods

There is one more important concept in partial classes called as partial methods. Partial methods helps us to define a method definition in one of the physical files and we can implement that method in the other physical files as shown in the below figure.

In the below figure you can see we have defined "Validate" method in "Customer1.cs" and this validate method is implemented in "Customer2.cs". Please note the partial keywords attached to both of these methods.

3.jpg

Use number 1:- ASP.NET auto generated code


The biggest use of partial classes is in technologies where there is code generation. Microsoft team themselves use partial classes in ASP.NET, LINQ and EF code generation. For instance when we look at ASP.NET there are two parts one is the auto generated code of a page and the other is behind code where you write your custom logic.

The custom logic is written in the ".aspx.cs" file while the auto generated logic is in ".aspx.designer.cs" file as shown in the below figure.

4.jpg

As a developer you would like the auto generated code to do his work i.e. generate code when you drag and drop a button the ASP.NET designer.

5.jpg

Below is how the code snippet of the auto generated code looks like.

public partial class WebForm1 {

     

        /// <summary>

        /// form1 control.

        /// </summary>

        /// <remarks>

        /// Auto-generated field.

        /// To modify move field declaration from designer file to code-behind file.

        /// </remarks>

        protected global::System.Web.UI.HtmlControls.HtmlForm form1;

  

        /// <summary>

        /// Button1 control.

        /// </summary>

        /// <remarks>

        /// Auto-generated field.

        /// To modify move field declaration from designer file to code-behind file.

        /// </remarks>

        protected global::System.Web.UI.WebControls.Button Button1;

       

        /// <summary>

        /// Label1 control.

        /// </summary>

        /// <remarks>

        /// Auto-generated field.

        /// To modify move field declaration from designer file to code-behind file.

        /// </remarks>

        protected global::System.Web.UI.WebControls.Label Label1;

    }

At the same time you would also like to customize the code in some other file so that the auto generation part is not disturbed. For the same ASP.NET provides the ".aspx.cs" file which is a partial class where in you can go put your own custom logic.



7.jpg


8.jpg



Use number 3:- Better maintenance by compacting large classes 

The other important use of partial classes is for better maintenance of the project. If you have large classes with lots of methods as shown in the below figure , it's a bit pain to maintain those classes.

9.jpg

By using partial classes you can split them in to physical files as shown in the below figure thus making your project better and easy to maintain.

10.jpg

Use number 4:- Multiple people working on the same class

11.jpg


The last and final real time I see of partial classes is when we want simultaneously two developers to work in the same class. I agree this can be a very rare use as there are better options like using a version control software like TFS or Sub version, but in case you want something quick and local this option is not bad at all.

You can also watch my 500 videos on different technologies like .NET, C#, Silverlight, Azure, VSTS, WCF, WPF, WWF, Share Point, design patterns, UML and lots more.

Login to add your contents and source code to this article
share this article :
post comment
 

nice hordwork

Posted by s v Feb 29, 2012

Hi. You have presented your article in a very nice way. Thanks for sharing this.

Posted by Monika Arora Jan 13, 2012
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Team Foundation Server Hosting
Become a Sponsor