Partial Class Accessibility Merging: Uncertain Permission Boundary
25K reputation · 10 May 2022, 03:09 UTC
Goal
Ensure that all parts of a partial class share the same accessibility modifier so that an unintentional public exposure does not occur.
Constraints
The current C# compiler (Roslyn) silently merges partial class parts, taking the most permissive modifier without any warning or error. This behavior can cause a type intended to be internal to become public if any part is declared public.
Unresolved Decision
There is no documented compiler option or language feature that enforces uniform accessibility across partial class parts. Static analysis tools can detect mismatches, but they are not part of the language specification.
Questions
- Does any existing or upcoming C# compiler version provide a flag to warn or error on mismatched accessibility in partial classes?
- Can Roslyn analyzers be configured to treat such mismatches as compile‑time errors instead of warnings?
- Is there a planned language change that would alter the current permissive merging rule for partial class accessibility?