A partial class in C# is a compile-time language feature that allows the declaration of a single class, struct, interface, or record to be divided across multiple physical source code files. During the compilation process, the C# compiler aggregates all constituent parts and emits them as a single, unified type definition within the resulting Intermediate Language (IL). The Common Language Runtime (CLR) has no concept of partial types; it only executes the merged, final type. To define a partial class, theDocumentation Index
Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
Use this file to discover all available pages before exploring further.
partial contextual keyword is applied as a modifier to the class signature in every participating file.
File1.cs
Compilation Rules and Constraints
The C# compiler enforces strict rules when merging partial class definitions. Violating any of these constraints results in a compile-time error.- Keyword Requirement: Every part of the type must explicitly include the
partialmodifier. - Assembly and Module: All parts of the partial class must be compiled into the same assembly (
.dllor.exe) and the same module. Partial classes cannot span across different assemblies. - Namespace: All parts must reside within the exact same namespace.
- Access Modifiers: If an accessibility modifier (e.g.,
public,internal,private) is explicitly specified on multiple parts, those modifiers must match exactly. It is not required to explicitly declare the access modifier on every part; if omitted on a part, it inherits the accessibility defined by the other parts. - Type Modifiers: If any single part is declared
abstractorsealed, the entire merged class inherits that restriction. - Base Classes: A class can only inherit from a single base class. If multiple parts specify a base class, they must specify the exact same base class. It is not required for all parts to specify the base class, as long as one does.
- Interfaces: Interface implementations are cumulative. If Part A implements
IDisposableand Part B implementsIEquatable<T>, the merged class implements both.
Member Merging Behavior
When the compiler merges the partial definitions, the resulting type contains the union of all members declared across all parts.- Fields and Properties: All fields, properties, and events from all files are available to the entire class.
- Attributes: Attributes applied to the individual parts are combined. If Part A has
[Serializable]and Part B has[Obsolete], the compiled class will possess both attributes. - Nested Types: Partial classes can contain nested partial classes. The merging rules apply recursively to the nested types.
Partial Methods
Partial classes can also contain partial methods. This feature allows one part of a partial class to define a method signature (the declaration) while another part provides the method body (the implementation).void, cannot have out parameters, and are implicitly private. (Note: C# 9.0 introduced extended partial methods that bypass these restrictions, provided they have an accessibility modifier, which forces the compiler to require an implementation).
Master C# with Deep Grasping Methodology!Learn More





