Glossary / Partial types

Partial types

A partial type is declared across multiple source files using the partial modifier, and the compiler combines the declarations into one type during compilation. Partial classes, structs, interfaces, records, and methods help separate generated code, designer code, or cohesive implementation files without creating runtime type boundaries.

Combination rules

All declarations must use partial, agree on the type kind and type parameters, and be compiled into the same assembly. Accessibility and base/interface declarations must be compatible; partial declarations can contribute interfaces and members. Attributes from parts are combined according to compiler rules. A partial method has special erasability/implementation rules that have evolved; modern partial methods can have accessibility and non-void returns when an implementation is required.

Source generators commonly emit partial declarations so user code can add behavior. The generated and authored files still form one type and can access its private members. Partial types do not create separate deployment units and can hide excessive coupling if used simply to split a large class arbitrarily.

Engineering guidance

Use partials for generated code integration or clear separation of platform-specific or cohesive concerns. Keep generated output distinguishable and do not edit generated files. Ensure all parts compile in every target framework and generator configuration. Prefer decomposition into collaborating types when responsibilities need independent ownership or testing.

Interview questions

  1. Do partial declarations create separate runtime types? No; they are combined at compile time.
  2. Can parts be in different assemblies? No; they must participate in the same compilation/assembly.
  3. Why source generators use partial types? They add implementation to a user-declared type while accessing its members.
  4. What must match? Type kind/name/parameters and compatible modifiers/constraints/accessibility.
  5. When split into separate classes instead? When responsibilities need independent boundaries and lifecycles.

References