A C# partial class is a functionality of C# that allows the implementation of a class in multiple files. Even if the class is defined into multiple files the compiler will combine the implementation of the class into a single class. Implementation of a partial class is done using the partial keyword.
Implementation of RecordPartialDef.cs
public partial class RecordPartial
{
int RecordId { get; set; }
string Name { get; set; }
string Surname { get; set; }
}
Implemention of RecordParitalImp.cs
public partial class RecordPartial
{
public void AddRecord() { }
public void DeleteRecord() { }
}
When to use a partial class?
In the following section, we will cover the different use cases when to opt for a partial class.
- The most used use case of partial classes is when code is being auto-generated. A partial class gives us the opportunity to easily extend a class that has been generated through code. Code that is modified manually is not overwritten if the code is regenerated. This allows segregation of concerns between auto-generated code and code extensions.
- A useful feature of partial classes is in a scenario when a class implements multiple interfaces. To make code more readable to can implement each interface into a separate file.
- Refactoring of God classes. A god class is a design pattern where a class is responsible for a lot of things. Such classes are hard to manage as they will contain a lot of code. Splitting a God class into multiple files will allow you to segregate responsibilities into separate files. The added advantage is that it simplifies the processes of refactoring a God classes without going through a shotgun surgery.
- Partial classes simplify the collaboration between multiple developers as multiple developers can be working on the same class but manipulating different files. Reducing the time-consuming process of merging conflicts.
Partial Class Rules
- The partial modifier must be placed before the class, struct or interface keywords
- A partial class has to reside in the same namespace and assembly.
- All the partial classes that build the class must be assigned the same accessibility ie. all public, private, protected.
- Different classes can inherit from different base classes or implement different interfaces. The final class will be the aggregate of all the classes.
- You can implement nested partial classes
- If one of the partial classes is marked as abstract or sealed then the resulting class will be assigned the class type.
Partial functions
A partial class can contain a partial function. As with the partial classes and partial function require a partial modifier before the function name. A partial function allows a developer to define a signature of a function that can be implemented in another partial class. The implementation of the partial function is optional. The compiler will remove the function from the resulting class if there is no implementation.
Partial functions are simplicity private and you cannot assign them a different modifier.
Partial functions require a void return type.
Partial functions support in or ref parameter but not out parameters.
public partial class MyPartialClass
{
partial void MyPartialFunction();
}
public partial class MyPartialClass
{
// Implementation partial
void MyPartialFunction() { } }
}