Partial Classes in C#

This article explains partial classes in C# and .NET. We will also learn why we need partial classes followed by how to create a partial class and put it to work in a real application.

Introduction

PartialClasses.png
Partial classes were introduced in C# 2.0 and later extended in C# 3.0.

  1. function test(){
  2. console.log("Hello world!");
  3. }
  4. (function(){
  5. var box = function(){
  6. return box.fn.init();
  7. };
  8. box.prototype = box.fn = {
  9. init : function(){
  10. console.log('box.init()');
  11. return this;
  12. },
  13. add : function(str){
  14. alert("add", str);
  15. return this;
  16. },
  17. remove : function(str){
  18. alert("remove", str);
  19. return this;
  20. }
  21. };
  22. box.fn.init.prototype = box.fn;
  23. window.box =box;
  24. })();
  25. var testBox = box();
  26. testBox.add("jQuery").remove("jQuery");

1.png

It is very common for class library developers or architects to define and implement a lot of functionality in a single class. For example, a Car class can hold the definition and all of its functions. What if the functionality is too much? Obviously, the file becomes large and it becomes cumbersome to use and to be updated by developers. What if multiple developers in a team want to modify the same class? What if the Car class has functionality separated by its external interface, internal design, and the engine and three separate developers work on three different aspects of this class?

The solution to this problem is partial classes. We can break down the Car class into three partial classes. Each partial class can be a different source file (physically). In the case of the Car class, there can be three physical files with even different names, CarExternal.cs, CarInternal.cs and CarEngine.cs, all of them defined in a Car class. When all these three partial classes are compiled, they are compiled as one Car class. But the advantage here is that three separate developers can work on three different files without affecting one another.

The preceding diagram shows two images. The first image shows an example of a typical single class Car that resides in a single Car.cs file. The second image is an example of three partial Car classes that reside in three different .cs files.

In some project types, Visual Studio separates the same class into two partial classes to separate the user interface with code behind so the designers and programmers can work on the same class simultaneously.

Not only the definition of a class, but struct, interface, and a method can be implemented as partial and split over multiple source files.

Creating Partial Classes

Let’s take a quick look at the Car class in Listing 1.

  1. public class Car
  2. {
  3. // Car Exterior Functionality
  4. public void BuildTrim() { }
  5. public void BuildWheels() { }
  6. // Car Interior Functionality
  7. public void BuildSeats(){}
  8. public void BuildDashboard() { }
  9. // Car Engine
  10. public void BuildEngine() { }
  11. }

Listing 1

The code in Listing 1 defines the Car class that has its functionality broken down into three parts, Exterior, Interior, and Engine.

  1. <h1>{{title}}</h1>
  2. <h3>{{subtitle}}</h3>
  3. <h4>
  4. Current Date and Time: {{datetime | date:'yyyy-MM-dd hh:mm'}}
  5. </h4>

The partial keyword is used to create a partial class. All the parts must use the partial keyword. All the parts must be available at compile time to form the final type. All the parts must have the same accessibility, such as public, private, and so on.

  1. ul {
  2. max-width: 800px;
  3. color: #cc4871;
  4. margin: 20px auto;
  5. padding: 2px;
  6. }
  7. .list li {
  8. padding: 20px;
  9. background: #f8d8f2;
  10. border-radius: 12px;
  11. margin-bottom: 12px;
  12. text-align: center;
  13. font-size: 12px;
  14. }

Now, we are going to break the Car class down into three partial classes to make it simpler.

  1. public partial class Car
  2. {
  3. // Car Exterior Functionality
  4. public void BuildTrim() { }
  5. public void BuildWheels() { }
  6. }
  7. public partial class Car
  8. {
  9. // Car Interior Functionality
  10. public void BuildSeats() { }
  11. public void BuildDashboard() { }
  12. }
  13. public partial class Car
  14. {
  15. /// Car Engine
  16. public void BuildEngine() { }
  17. }

Listing 2

All partial classes in Listing 2 reside in multiple .cs files.

Partial Methods

A partial class or struct may contain a partial method. Similar to a partial class, a partial method can be used as a definition in one part while another part can be the implementation. If there is no implementation of a partial method then the method and its calls are removed at compile time.

A partial method declaration consists of two parts: the definition and the implementation.

The code listing in Listing 3 defines a partial method called InitializeCar in a different .cs file and the implementation of the same method is in a different .cs file.

  1. public partial class Car
  2. {
  3. // A partial method definiton
  4. partial void InitializeCar();
  5. // Car Exterior Functionality
  6. public void BuildTrim() { }
  7. public void BuildWheels() { }
  8. }
  9. public partial class Car
  10. {
  11. /// Car Engine
  12. public void BuildEngine() { }
  13. // A partial method implementaion
  14. partial void InitializeCar()
  15. {
  16. string str = "Car";
  17. // Put all car initialization here
  18. }
  19. }

Listing 3

More

The following are some more articles on partial classes.