C# Reference ยท Keywords and Statements
The partial keyword
Reference for C# partial: split a class, struct or interface across files, partial methods with and without bodies, partial properties and generated code.
partial lets you split the definition of one type across several declarations. The compiler merges them into a single type, as if you had written everything in one place. The declarations can live in different files, which is the main reason the feature exists: a code generator can own one file while you own another, and neither overwrites the other.
partial also applies to methods and, since C# 13, to properties and indexers. A partial member has a defining declaration (the signature) and an implementing declaration (the body), usually in different parts of the type.
public partial class Invoice { public decimal Total { get; set; } } public partial class Invoice { public string Format() => $"{Total:F2}"; }
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.
Syntax
[modifiers] partial class Name { ... } [modifiers] partial struct Name { ... } [modifiers] partial interface Name { ... } [modifiers] partial record Name(...); partial void Hook(); // classic partial method [modifiers] partial ReturnType Method(parameters); // extended partial method (C# 9) [modifiers] partial Type Property { get; set; } // partial property (C# 13)
| Rule | Description |
|---|---|
| Same type | All parts must use partial, the same kind (class, struct, interface), the same name, namespace and type parameters, and be compiled together in one assembly. |
| Accessibility | If parts state an access modifier, it must agree. Modifiers such as sealed and abstract on any part apply to the whole type. |
| Base types | Each part may list base types or interfaces; the type gets the union. If two parts name different base classes, it is an error. |
| Members | The merged type has every member of every part. Duplicate member names clash as usual. |
| Classic partial method | Must return void, have no out parameters and no access modifier. It is private, and if no part implements it, calls to it vanish. |
| Extended partial method | Any accessibility, return type, out parameters and modifiers such as virtual or async; an implementing part is required. |
Errors: a missing partial on one part is CS0260 ("Missing partial modifier"); conflicting base classes are CS0263; an extended partial method without implementation is CS8795.
Usage notes
- Generated code. Designers, source generators and tools such as the
[GeneratedRegex]and[LoggerMessage]attributes produce the second part of apartialmethod or class. Your hand-written part declares the signature. - Use sparingly. Splitting a huge class across files hides its size without making it simpler. Use
partialfor generators and for separating nested concerns, not as a replacement for refactoring. - Compile-time only. Partial types leave no trace at run time; the result is an ordinary class.
- Attributes and XML comments on all parts are combined.
- Contextual keyword.
partialis a keyword only right beforeclass,struct,interface,record,voidor a return type, and it must appear immediately before the keyword that follows the other modifiers (public partial class). - Nested types can be partial too, as long as every containing type is also partial.
- Partial properties (C# 13) have a defining declaration
public partial string Name { get; set; }and an implementing declaration with bodies or accessors. They allow generators to supply the body of an auto-property-like member. - Top-level
Program. The compiler-generatedProgramclass from top-level statements is declared partial, so you can add members withpartial class Program.