Answer to the Core Questions
1. No compiler flag exists because the C# compiler already treats mismatched accessibility modifiers in partial class parts as a compile‑time error (CS0260). The compiler does not silently merge the parts; it refuses to build until all parts agree on the same modifier.
2. Roslyn analyzers can be configured to treat the mismatch as an error by elevating the analyzer’s diagnostic severity. The built‑in CS0260 diagnostic can be promoted via editorconfig or a custom analyzer that flags the same pattern and sets the severity to error.
3. No planned language change is documented that would alter the current rule. The C# specification (§10.2) explicitly requires identical accessibility for all partial type declarations, and this has been stable across releases.
Why the Compiler Errors
Unlike some languages, C# enforces a single accessibility boundary for a type. If one part is public and another internal, the merged type would have an ambiguous visibility that could expose internal members to the outside world. The compiler therefore emits CS0260:
CS0260: Partial declarations of 'MyClass' must not specify different accessibility
To avoid this, all parts must use the same keyword (or omit it, which defaults to private and must match the others).
Practical Steps for Your Project
- Locate all partial declarations. Search the solution for
partial class MyClass.
- Verify the accessibility keyword. If a part omits it, remember the default is
private.
- Align the modifiers. Make every declaration use the same keyword (e.g., all
internal).
- Rebuild. The error should disappear. If it persists, double‑check nested types or partial structs/enums.
- Optional: Enforce via
editorconfig. Add:
[*.cs]
dotnet_diagnostic.CS0260.severity = error
This guarantees the compiler treats the diagnostic as an error even if you later change the default severity.
When an Analyzer Might Be Needed
If you prefer a linting experience that flags mismatches before compilation (e.g., in a CI pipeline), you can write a simple Roslyn analyzer that scans for partial declarations with differing accessibility. The analyzer can emit a diagnostic with severity error or warning as you wish.
Missing Diagnostic Detail?
If you encounter a build that succeeds despite mismatched modifiers, double‑check that you are using the latest .NET SDK and that no custom build step is altering the compiler flags. The behavior described above is standard in all recent releases (from .NET 5 onward).