Understanding C# Partial Classes and Methods
Understanding C# Partial Classes and Methods
This article explains how and why C# developers use partial classes and partial methods to manage large codebases, collaborate effectively, and optimize compilation.
When multiple developers work on a single, extensive class, or when you need to merge hand‑written code with code generated by tools like Visual Studio, C#’s partial keyword offers a clean solution.
What is a Partial Class?
A partial class lets you split the definition of a single class across two or more source files. Each file contains a fragment of the class, and the compiler stitches them together at build time. The keyword partial must appear in every part of the class.
Example 1 – Splitting a Simple Class
Consider a project named HeightWeightInfo that stores a person’s height and weight.
namespace HeightWeightInfo
{
public partial class Record
{
private int h;
private int w;
public Record(int h, int w)
{
this.h = h;
this.w = w;
}
}
}
The same Record class is continued in a second file.
namespace HeightWeightInfo
{
public partial class Record
{
public void PrintRecord()
{
Console.WriteLine("Height:" + h);
Console.WriteLine("Weight:" + w);
}
}
}
Finally, the program that uses the class looks like this:
namespace HeightWeightInfo
{
class Program
{
static void Main(string[] args)
{
Record myRecord = new Record(10, 15);
myRecord.PrintRecord();
Console.ReadLine();
}
}
}
The compiler merges the two Record definitions into a single class that contains both the constructor and the PrintRecord method.
When to Use Partial Classes
- Large projects with multiple developers: Each team member can edit a separate file without conflict.
- Auto‑generated code: IDEs often create files that should not be manually edited; partial classes allow you to add custom logic without modifying generated code.
Key Points to Remember
- The
partialkeyword must appear in every file that contributes to the class. - All parts must reside in the same namespace and be compiled together.
- All parts must share the same access modifier (e.g.,
public,internal). - If any part declares the class as
abstractorsealed, the entire class inherits that attribute. - All members defined in any part are visible to every other part.
- Partial modifiers are not allowed on delegates or enumerations.
What is a Partial Method?
A partial method is a special type of method that can be declared in one part of a partial class and optionally implemented in another. If the implementation is omitted, both the method declaration and any calls to it are discarded at compile time, which can improve performance.
Example 2 – Using Partial Methods
In file1.cs, the Car class declares a partial method and other regular methods.
public partial class Car
{
partial void InitializeCar();
public void BuildRim() { }
public void BuildWheels() { }
}
In file2.cs, the same Car class provides the implementation for InitializeCar and adds another method.
public partial class Car
{
public void BuildEngine() { }
partial void InitializeCar()
{
string str = "Car";
}
}
Partial method declarations consist of two parts: the signature in one file and the optional implementation in the same or another file.
Rules for Partial Methods
- Must be declared with the
partialkeyword. - Return type is always
void(no return value). - Implicitly
private; they cannot bepublic,protected, orinternal. - Cannot be declared
virtual,override, orabstract. - If no implementation is provided, the compiler removes the method and any calls to it.
C Language
- C# Classes & Objects: Foundations for Robust OOP
- C# Methods Explained: Declaration, Calling, Parameters, Return Types & More
- Mastering C# Abstract Classes & Methods: A Practical Guide
- Master sealed classes and methods in C#—learn when to use them, see clear examples, and boost code safety and performance.
- C++ Classes & Objects: A Practical Guide to Object‑Oriented Programming
- C++ Friend Functions and Friend Classes: Mastering Access Control
- Java Classes and Objects: A Practical Guide
- Java Abstract Classes and Methods: A Comprehensive Guide
- Mastering Java Nested and Inner Classes: Types, Examples, and Best Practices
- C++ Classes & Objects: A Practical Guide with Code Examples