From the December 2015 Issue of Prestwood eMag
Coder Object Orientation (OO):
Partial Class
Posted 1/5/2009 on 1/5/2009 and updated 2/9/2009
Take Away:

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler. The compiler puts the class together at compile time and treats the final class or type as a single entity exactly the same as if all the source code was in a single location.

Coder Definition Flashcard:


Partial Class -

A partial class, or partial type, is a class that can be split into two or more source code files and/or two or more locations within the same source file. Each partial class is known as a class part or just a part. Logically, partial classes do not make any difference to the compiler. The compiler puts the class together at compile time and treats the final class or type as a single entity exactly the same as if all the source code was in a single location.

Partial classes are primarily a source code convenience feature that allows multiple developers to work on the same class and allows you to organize a class' code within a source file or among two or more source files. For example, you could separate a class' code into UI-code, business-code, etc.

Origin: Class Extensions

Partial classes have been around in Smalltalk for a considerable time but are known as Class Extensions.

The following is from the blog post What's Good About Smalltalk by Alan Knight (a Smalltalk advocate):

"Class Extensions. Original Smalltalk just had no concept of packages/modules/whatever. There was the image, it was one big blob, and it was intended to enhance the productivity of the individual. Teams need not apply. Team programming systems soon emerged, but they all included the idea of the "class extension" - the ability for a package to define additional methods in a class that was outside that package. This is a very powerful ability, and one that Smalltalkers sorely miss in other environments. We're beginning to see traces of this in other environments - "partial classes" and the like..."
"...Of course, this power is a two-edged sword. Being able to extend any class in the system often means being able to crash the system."

Uses for Partial Classes

The following are some usage examples for partial classes:

  • Separate Code Generator Code: Another interesting use case is in a code generation scenario where a code generator can generate one part and a developer can maintain another part without disturbing the generated code (and vice versa). Starting with version 2, C# splits form designer code into two files. One for designer generated code and one for your code. It no longer mixes designer generated code with your code.
    Suggested Link: The Practical Uses of Partial Classes in Visual Studio 2005 article by Wei-Meng Lee on talks about how Microsoft used partial classes in VS.Net 2005 to hide designer-generated code.
  • Organize Your Code: You could split a large class into two or more categories. For example, by member type (methods, properties, member fields, etc.), by inheritance/interface type (implemented abstract members, new members, overridden members, implemented interface members, etc), or by some other logical division such as functionality. Now, I realize that a class should deal with one and only one "thing" and a large class sometimes means bad design, but sometimes a very large class is what is called for and partial classes can be used to better organize the code.
  • Multiple Developer Development: For multi-developer teams working on actively developed large classes, it can be very helpful. You could split a class so that two or more developers can work on it at the same time. Version control systems can accomplish about the same thing but with a partial class, the developers don't even have each other's code and a merge is not needed.
  • Different Versions: You could have a class that has extra features under certain circumstances. What if you are selling a program and you want to add features for paying customers yet maintain the same code base for the free version. There are several ways to implement this but one of them is with partial classes. You could split a class between common code (always included) and one of two other files that you switch at compile time either with a compiler directive or by physically moving the file in or out of the source folder. Compiler directives would probably be cleaner so that you could split a Cyborg class among CyborgCommon.cs, CyborgFree.cs, and CyborgPaid.cs and use compiler directives to compile either the free or paid versions of the exact same class. The common code for both the free and paid versions of the class are in a common CyborgCommon.cs source file.
  • Multiple Language Classes: In .Net, for example, you can use partial classes to code a single class in one or more .Net languages. Although you probably should avoid this capability in general, in a specific situation it may be highly desirable to use partial classes to have your developers working in the their chosen language on a specific group of methods in a specific class. However, for the vast majority of classes, you will not need partial classes. It should be reserved for when it's called for. In addition, when it is called for your first choice should be to keep an entire class in a single language. Reserve partial classes for design decisions, not language decisions. After all, elegant coding means your code is easier to maintain and enhance. Working on existing code in a single language is easier than working on code in two or more languages.

Partial classes and .Net

Microsoft added the concept of partial classes to the .Net CLS in C# 2.0 and VB.Net 2005 (along with the arrival of the .Net Framework 2). With the .Net implementation, you can split a class into multiple source files, and/or multiple locations in the same source file so long as they are all in the same namespace. All parts must use the same base class so it's typical to indicate the base class in only the first part. All other parts don't need to specify the base class. All the parts included at compile time are compiled. This presents some interesting possibilities.

.Net Features

  • Parts can specify different base interfaces, and the final type implements all of the interfaces listed by all of the partial declarations.
  • Any class, struct, or interface members declared in a partial definition are available to all of the other parts.

.Net Limitations

  • C++/CLI does not yet support partial classes.
  • All parts must be defined within the same namespace.
  • All the parts must use the partial keyword.
  • All of the parts must be available at compile time to form the final type.
  • All the parts must have the same accessibility (i.e. public, private, etc.).
  • If any of the parts are declared abstract, then the entire type is abstract.
  • If any of the parts are declared sealed, then the entire type is sealed.
  • If any of the parts declare a base type, then all parts use that same base type. All parts that specify a base class must specify the same base class. You can omit a base class in one or more of the parts in which case the part still uses the same base class.

What about Java and standard C++?

Neither support partial classes as of this writing. In C++, you can split the implementation of a class among two or more source files. However, you must declare the member in the class declaration.

Are partial classes a good idea?

Like all language features, it's good if it's used well. In the hands of a careful and talented software designer, you will end up with cleaner more elegant code that is easier to maintain and enhance. That's the bottom line.

In the wrong hands, it can easily be abused. If over used, it may not be clear where to go to edit some source code and in essence break the separation of concern rule. A class should deal with one and only one concern. A concern is any piece of interest or focus of a program. Usually a set of features or behaviors.

One of the nice benefits of OOP is that you only need concern yourself with a class' interface and usage. If you have to open up the black box, fix or enhance a class, you generally have one source file to look at and sometimes just one method. With partial classes you may have to look at two or more locations within the same source file and/or look at two or more source files perhaps even at two or more code languages. Not a big deal, but as always you should take care to organize your code.

Partial Methods

In addition to partial classes, some languages support partial methods. A partial method is an empty method defined in a partial class. Both partial methods and abstract methods are very similar in usage. However, a partial method is a callable empty method whereas an abstract method is a defined method in a parent class that must be implemented in a child class. Contrast this with a partial method which can be implemented in a child class but does not have to be. Partial methods are common with code generators for managing events.

Partial Classes versus Class Helpers

Class helpers, Delphi implementation, allow you to extend a class without using inheritance. With class helpers, you do not have to create and use a new class descending from a class but instead you enhance the class directly and continue using it as you always have. Like partial classes, class helpers are primarily a source code control feature. A class helper tells the compiler to increase the compile scope when resolving identifiers. In very general terms, class helpers allow you to easily extend a class when inheritance isn't possible or practical. Contrast with partial classes which are used for source code management with code generators and large classes to split ownership of a class. Class helpers allow you to cheat a bit while partial classes allow you to divide and conquer.

Note: None of the .Net languages currently support class helpers (it's a Delphi-specific feature right now for building native code Win32 applications). Although both class helpers and partial classes are controversial, I hope a future version of the various .Net languages will support both. I think there is a place for both class helpers and partial classes in OO languages. In the right hands, I think both features lend themselves to more elegant code that is easier to maintain and enhance.

Definition Contributed By Mike Prestwood:

Mike Prestwood is a drummer, an author, and creator of the PrestwoodBoards online community. He is the President & CEO of Prestwood IT Solutions. Prestwood IT provides Coding, Website, and Computer Tech services. Mike has authored 6 computer books and over 1,200 articles. As a drummer, he maintains and has authored 3 drum books. If you have a project you wish to discuss with Mike, you can send him a private message through his PrestwoodBoards home page or call him 9AM to 4PM PST at 916-726-5675 x205.

Visit Profile For service: 916-726-5675
Copyright (C) Prestwood IT Solutions.
All Rights Reserved.
Printed 1/27/2022