Understanding and Using Partial Classes in C#
page 4 of 8
by Brendan Enrick
Feedback
Average Rating: 
Views (Total / Last 10 Days): 82053/ 148

When to Use Partial Classes

There are plenty of cases where a partial class can assist you in developing your applications. Most people have already seen them and might not have noticed them in ASP.NET pages, because you don't actually need to know that it is using these partial classes in order to use them. They're also quite popular being used alongside generated code, because it lets people modify the generated code. Sometimes people will also use it when working in larger groups who will not be committing code often. This is because it lets them make large amounts of changes without having painful merges later.

ASP.NET Pages Already Use Partial Classes

First I will remind you again of the ASP.NET pages. Every page you create in ASP.NET is eventually compiled. Every .aspx file will be used to declare a page class. They all inherit from a standard Page class. You can see this in your code behind file. If you view the code of your ASP.NET page you will see a class definition. It will have the partial keyword in the definition and it will look similar to this code.

Listing 3: Codebehind File

public partial class MyWebPage : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    // Perform some action here.
  }
}

Since there will be a class created from your ASP.NET code in the .aspx file this must be a partial class. This brings up one of the best uses of partial classes. You should use partial classes when you want to write code for a class which is auto-generated. Since the code for the page class is generated for you, it is important to keep your code separate. This means that a partial class is perfect, because it keeps the code physically but not logically separate. Being in a separate file means the auto-generating process will not kill your code and the compiler will still know to add in your code before it compiles the code.

Working with Generated Code

Some developers use software to generate code for them, and this will let them modify the code for their own purposes without making changes to the generated code. I use LLBLGen, and it generates a ton of code for me. When I need to make modifications I can't go in and edit the files it creates, because the next time I generate the files my changes will disappear. Instead I create a new file for each class I want to extend. I then add in the definition for a partial class. This file I have created will not be overwritten by LLBLGen since LLBLGen didn't create the file, and the compiler will still make sure that my code is compiled into the class.

Alongside generated code is likely the most common place where partial classes are used. Since ASP.NET pages use it, and it works so well it just makes sense to use partial classes with generated code. If you ever find generated code which is not using partial classes, make sure to mention to the person, group, or company who made the generator about your request that the classes be written as partial classes. It will make your code better organized, easier to work with, and safe from being overwritten.

Multiple Developers Working On the Same Classes

It gets annoying sometimes when writing code with multiple developers. If the developers will be working without or with minimal source management this can be very necessary. I often run into problems having to merge changes with other people. This happens because most developers I know add new code to the bottom of the file. This is probably because it is easy to add code there without having to think about placement. It gets annoying after a while when everyone does this, and sometimes merging tools get confused and I end up merging by hand anyway. I've even had auto-merge make mistakes when merging and I've obtained code which does not compile. Sometimes it is easier to just have partial classes for this. This does add a small maintenance headache, and you might be thinking that it becomes difficult to find the correct code. I never have a problem finding the correct code in the file, and this is because of a little tool we all know and love. It is called "Go To definition". For those of you who somehow missed this important tool I've included a nice screenshot. Just right click on the named piece of code and you will see these options. Click on "Go To Definition".

Figure 1: Go To Definition

I really hope that everyone knows how to do this already. If you don't know how to use this feature then you should learn it quickly. Your life will become much better. When you use this you will not need to remember where the code is because Visual Studio will take you directly to the location you want.

When you're working with multiple people and not using a versioning system often, it becomes quite difficult to manage the changes without painful merges. As long as you're not working on tightly knit code, it is easier to just keep the code in separate files. Sure, files become a little bit more of a pain to manage, but it is very nice to not have to merge changes in files after extensive changes in the code.


View Entire Article

User Comments

Title: sizar   
Name: sizar
Date: 12/1/2012 1:43:44 AM
Comment:
please check this website for more information http://fantasyaspnet.blogspot.in/2012/11/write-program-in-c-to-implement.html
Title: 1   
Name: 1
Date: 11/15/2012 6:38:39 AM
Comment:
1
Title: *   
Name: *
Date: 9/6/2012 9:38:19 AM
Comment:
*
Title: partial class   
Name: Bijay
Date: 9/4/2012 12:52:54 PM
Comment:
Thanks ,but not enough.
Title: Partial Class   
Name: Manoj
Date: 8/9/2012 9:19:03 PM
Comment:
good . Easy to understand !!!
Title: w   
Name: w
Date: 7/2/2012 6:25:22 AM
Comment:
q
Title: sfd   
Name: sdf
Date: 6/12/2012 5:14:19 PM
Comment:
sdf
Title: Awesome   
Name: Vinod
Date: 5/15/2012 1:07:46 AM
Comment:
Very Good and helpfull
Title: thanks   
Name: ajith
Date: 8/26/2011 7:54:19 AM
Comment:
super discription
Title: thanks   
Name: shaheen
Date: 1/13/2011 1:13:12 AM
Comment:
good........
Title: good   
Name: smithu valsalan
Date: 1/13/2011 1:07:37 AM
Comment:
very useful article
Title: kumar   
Name: umesh
Date: 12/16/2010 2:36:39 AM
Comment:
excellent....!
Title: Excellant Artical   
Name: Haribabu Nimmagadda
Date: 7/22/2010 7:58:00 AM
Comment:
It helps me alot.
Title: Good one!   
Name: Lakshmi Sudheendra
Date: 5/26/2010 8:53:17 AM
Comment:
Very useful article. Described in simple words and optimally covered the essential topics.
Title: excellent   
Name: vasudev
Date: 5/22/2010 6:10:46 AM
Comment:
excellent jestification
Title: I like the justification   
Name: george
Date: 2/24/2010 10:20:43 AM
Comment:
I was aware of the autogenerated code case, but code merging is the real day to day convenience this feature provides.
Title: very good explanation   
Name: krishna kishore
Date: 2/16/2010 9:29:59 AM
Comment:
your explanation is very clear. and simple
Title: Wonderfull   
Name: s.arul
Date: 2/9/2010 8:17:46 AM
Comment:
Wonderful article for asp.net beginers
Title: Good   
Name: Sanjeev
Date: 2/9/2010 5:29:54 AM
Comment:
Here in ur article u have described enough.. but plz try to add more examples so it will clear the doubts of readers.. all the best
Title: Good   
Name: Laxmi
Date: 12/22/2009 8:04:24 AM
Comment:
Its good but I have a question.I have a class which is splitted into two partial classes.These two partial classes have same methods.what happens at compile time.Please help me.thanks in advance..
Title: Nice Article   
Name: Senthil
Date: 11/2/2009 6:07:40 AM
Comment:
Very Nice Article!!
Title: Excellent !!   
Name: Sai
Date: 10/30/2009 3:10:32 AM
Comment:
Nice article for beginners.
Title: partial class   
Name: vikash kumar
Date: 10/15/2009 6:07:41 AM
Comment:
Article written by you is good but not enough.pls modify it.
Title: Great little read   
Name: Piao!
Date: 9/18/2009 11:34:43 AM
Comment:
Good writing style, and nice real world examples - cheers man!
Title: Nice Article   
Name: sam
Date: 9/9/2009 7:42:56 AM
Comment:
nice article Enrick. Can you suggest me some article on delegates & events?
Title: Good Article   
Name: Rajender Kumar
Date: 8/28/2009 12:49:41 AM
Comment:
i am a web developer Asp.Net C#. i have read this article, that is well understandig for beginners.
Thanks for taking time for this post.
Title: Thank you   
Name: phil
Date: 7/1/2009 8:01:46 PM
Comment:
Thanks for posting this. I despise .net but I have to do it at work so its nice to find articles that teach the concepts rather than syntax.
Title: confuseeeeeeeeee   
Name: sagar
Date: 1/29/2009 1:48:30 AM
Comment:
Hi.. i am cleared abt partial class in c#.... but u r article mix with aspx... ca u clarify this
Title: partial classes   
Name: shraddha
Date: 1/13/2009 4:12:44 AM
Comment:
very good article
Title: I am heap to get you   
Name: zinabu Alemayehu
Date: 11/8/2008 10:00:40 AM
Comment:
I need some help from you based on the asp,c#,and vb.if you get full of code about some project please send to me via my email.

I always Thank u.........
Title: Thank you   
Name: Shawn F.
Date: 9/23/2008 10:55:07 AM
Comment:
Thanks for taking the time to post this. It helps clear things up.

BTW...You're able to describe things well enough for me.






Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2014 ASPAlliance.com  |  Page Processed at 7/29/2014 4:52:36 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search