Blue Theme Orange Theme Green Theme Red Theme
 
Team Foundation Server Hosting
Home | Forums | Videos | Advertise | Certifications | Downloads | Blogs | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
Nevron Chart
Search :       Advanced Search »
Home » C# Language » Partial Classes in C#

Partial Classes in C#

In this article, I will explain what partial classes are, their benefits, and how to implement partial classes using C#.

Author Rank :
Page Views : 57104
Downloads : 1566
Rating :
 Rate it
Level : Beginner
   Print Read/Post comments Post a comment  Similar Articles  
   Email to a friend  Bookmark  Author's other articles  
Download Files:
PartialClass_code.zip
 
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
 Tag Cloud
 Latest Jobs
More ... 
 Latest Interview Questions
More ... 


Partial Class

 

In this article I will explain what is a partial class? What are the benefits of using partial classes and how to implement partial classes in your C# applications. 

 

Partial class is a new feature added to C# 2.0 and Visual Studio 2005. It is supported in .NET Framework 2.0. If you are working with .NET 1.0 or 1.1, partial classes may not work.
 

It is possible to split the definition of a class or a struct, or an interface over two or more source files. Each source file contains a section of the class definition, and all parts are combined when the application is compiled.

 

When working on large projects, spreading a class over separate files allows multiple programmers to work on it simultaneously.

 

When working with automatically generated source, code can be added to the class without having to recreate the source file. Visual Studio uses this approach when creating Windows Forms, Web Service wrapper code, and so on. You can create code that uses these classes without having to edit the file created by Visual Studio.

 

Benefit of partial classes:

 

1) More than one developer can simultaneously write the code for the class.

 

2) You can easily write your code (for extended functionality) for a VS.NET generated class. This will allow you to write the code of your own need without messing with the system generated code.

 

There are a few things that you should be careful about when writing code for partial classes: 

  • All the partial definitions must proceeded with the key word "Partial".
  • All the partial types meant to be the part of same type must be defined within a same assembly and module.
  • Method signatures (return type, name of the method, and parameters) must be unique for the aggregated typed (which was defined partially).
  • The partial types must have the same accessibility.
  • If any part is sealed, the entire class is sealed.
  • If any part is abstract, the entire class is abstract.
  • Inheritance at any partial type applies to the entire class.

I have attached code of the partial classes along with this article. You can open the project and understand the functionality.

 

Hope the article would have helped you in understanding what partial classes are. Waiting for your feedback.

Comment Request!
Thank you for reading this post. Please post your feedback, question, or comments about this post Here.
Login to add your contents and source code to this article
 [Top] Rate this article
 
 About the author
 
Puran Mehra

Working as a Software professional. 

Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Discover the Top 5 .NET Memory Management Fundamentals
To write the best .NET code, you need to know exactly how the .NET framework really manages memory. Ricky Leeks presents the Top 5 fundamental facts of .NET memory management. Learn more.
Nevron Chart for .NET 2010.1 Now Available
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.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites – Click Here!
 
 Post a Feedback, Comment, or Question about this article
Subject:
Comment:
Nevron Chart
Become a Sponsor
 Comments
diego by diegoantonioguerrero On June 12, 2009
alert('hola amibuitos');
Reply | Email | Modify 
partial class by alex On June 17, 2009
When many people start working on the same class, it starts asking for bad code, bad design, bad productivity, hidden and obvious bugs...  When a class grows to the extent, that more than 1 developer needs to work on it, then, either the initial design was improper and it needs to be re-designned, or split into a few lesser ones, or that same developer should continue working on that and noone else. 

I don't know why VS designer splits form classes into 2 partial classes - same maintainability effect can be achieved by using #region def. 
Reply | Email | Modify 
nice article by Yatish On October 14, 2009
YATISHBHAVSAR
Reply | Email | Modify 
Good by Satheesh On May 13, 2010
Hi Puran,

A real good, now i understand what it is and y it is used,, thanks
Reply | Email | Modify 
Event Handlers by Frank On May 31, 2010
One thing about partial form and custom control classes I discovered the hard way (in VS2008) is that you need to keep the IDE generated event handlers in the original IDE generated file.  Otherwise the event browser won't be able to find them, and will put any new ones in the wrong place for your layout.  There are other repercussions, too -- like the IDE may try to generate a new event handler with the same name that's already used in another file(!)  Best just to move other things like properties and methods into partial classes that are located in other files.  Perhaps I've missed something, but I couldn't find any mention anywhere regarding this issue.  I ended up with a mess before I realized what was going on. 

Also be aware that if you use File/New/File/Visual C# Class to create the new partial class file, the IDE may not put it in the right directory with your other working files, but one level down.  This too can lead to confusion!

I agree with Alex about multiple developers, but personally I've found that usmg distinctive functional splits (like methods in one file, properties in another, enums in another) can make it much easier to get around, and keep pieces of code that work together immediately available.
Reply | Email | Modify 
Code Example by Aparna On June 4, 2010
Can you please give a code example too
Reply | Email | Modify 
Partial Class by Anil On August 10, 2010
Thanks Man.
Reply | Email | Modify 
partial classes by SatyaKamesh On August 20, 2010
Nice Article man . . . 

Keep on posting
Reply | Email | Modify 
Good One by prashant On October 26, 2010
MessageBox.Show("Wonderfull...!!!!");
Reply | Email | Modify 
Need help... by Mehaboob On March 2, 2011
how to make the methods partial class unique. i.e.User A working on partial class pcA and user B using the paritial class pcA which both may contain the same method in both the classes. how to handle this?
Reply | Email | Modify 
Nice Article!! by Rajesh G On June 17, 2011
Nice Article!!
Reply | Email | Modify 
Nevron Chart
 © 2012  contents copyright of their authors. Rest everything copyright Mindcracker. All rights reserved.