Partials¶
Partials allows us to split up "definitions" into multiple files. When compiled, the compiler combines all parts into a single class file.
Partial classes¶
- When working on large classes, allowing multiple developers to work on different parts.
- When using auto-generated code (e.g., UI frameworks like WPF, WinForms, Blazor).
- To organize related methods in separate files while keeping them under the same class.
Person.cs:
Person_2.cs:
Partial methods¶
A partial method is a method that is declared
(much like forward declarations in C++
) in one part of a partial class and optionally implemented in another part. If no implementation is provided, the compiler removes the declaration.
- Must be in a partial class
- Must return void (cannot return a value)
- Cannot have access modifiers (public, private, etc.)
- Cannot be virtual, abstract, or override
- Must be partial in both declaration and implementation
User_Declaration.cs:
User_Definition.cs: