Inconsistent accessibility error when exposing a file-scoped type from a public method
0 reputation · 18 Apr 2020, 14:13 UTC
When designing helper types that should not be accidentally exposed outside their source file, the file modifier offers stronger encapsulation than internal. However, if a member such as a public method returns or accepts that file‑scoped type, the compiler flags an accessibility‑domain inconsistency because the member’s effective domain is broader than the type’s domain. The decision to use file versus internal must consider future needs for partial declarations, source‑generated code, or cross‑file usage within the same assembly, as well as the required C# language version and SDK support.
What are the practical trade‑offs between file and internal for helper types that may later need partial declarations in another file? How does the accessibility‑domain rule behave when a source generator emits a file‑scoped type that is referenced from a member in a different file? Under what project constraints should a team prefer file despite the need for cross‑file accessibility?