Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Classes and Object-Oriented Concepts

Partial classes and methods in C#

Reference for C# partial types and members: split a class across files, partial methods with implementations, partial properties and constructors, and code generator use.

The partial modifier lets one type be declared in several pieces. The compiler merges all pieces in the same assembly into a single type, as if you had written them together. It works for classes, structs, interfaces and records. The usual reason is a code generator: the tool owns one file and you own another, so regenerating never overwrites your code.

Members can be partial too. A partial method has a declaration in one part and an implementation in another, which lets generated code call a method that you provide. Since C# 13 partial properties and indexers work the same way, and C# 14 adds partial constructors and events.

C#
partial class Invoice { public decimal Total { get; set; } }
partial class Invoice { public bool IsFree => Total == 0; }

Try it

C# One class, two parts

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.

C# Partial methods with a return value and partial properties

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

C#
partial class C { }                                   // part 1
partial class C { }                                   // part 2

partial void Hook();                                  // old-style partial method: private, void
public partial int Compute(int x);                    // extended partial method: any access, any return type
public partial string Name { get; set; }              // C# 13 partial property
public partial C(int x);                              // C# 14 partial constructor
Rule Description
Modifier Every part must say partial, appear in the same namespace and assembly, and use the same accessibility if any part states it.
Combining Base class, interfaces, attributes, XML comments and type parameter constraints from all parts are merged.
Old partial methods Return void, no access modifier, no out parameters. If no part implements it, the compiler removes the declaration and every call to it.
Extended partial methods Any accessibility modifier, return type, or out parameter: then an implementation is required.
Implementation Must match the declaration's signature, modifiers and attributes; a declaration may have only one implementation.

Errors: a missing implementation for an extended partial member gives CS8795 ("must have an implementation part because it has accessibility modifiers"), and two implementing declarations conflict.

Usage notes

  • Why it exists: source generators, designers (Windows Forms, WPF, Blazor) and serializers all emit partial classes so your hand-written code stays in a separate file.
  • Not for organizing big classes. Splitting a 3,000-line class into five files does not reduce its complexity. Prefer smaller classes or extracted types.
  • Partial method hooks cost nothing when unused. A call to an unimplemented void partial method disappears at compile time, including argument evaluation, so do not put side effects in the arguments.
  • Whole-assembly only. All parts must be compiled together; you cannot add a part from another assembly.
  • One part may specify the base type. The others can repeat the same, but they must not conflict. sealed and abstract on any part apply to the whole type.
  • Generator-friendly pattern: the generator declares public partial string Name { get; set; } and you supply the implementation (or the reverse).
  • Partial records and interfaces follow the same rules.