[Proposal] File-scoped types in explicit interface implementations. #8357
Replies: 2 comments 24 replies
-
How is this different from other non-public access levels, like public class PublicType
// This is already supported since C# 11.
: explicit IList<FileScopedType> // has to have "explicit" keyword (don't know if it should be before or after)
{
void IList<FileScopedType>.Add(FileScopedType arg)
{
throw null;
}
public void Add(FileScopedType arg) { } // compilation error: inconsistent accessibility, and
// if explicit method above is absent, also "method Add not implemented"
}
file class FileScopedType {
} |
Beta Was this translation helpful? Give feedback.
-
That is not correct. It can be accessed with generics. Consider the following: M(new PublicType());
void M<T>(IList<T> list) {
list.Add(null);
} In this case // file1
class Col: IEnumerable<FileType> {
public IEnumerator<FileType> GetEnumerator() => throw null;
IEnumerator IEnumerable.GetEnumerator() => throw null;
}
class FileType : IDisposable
{
public void Dispose() => throw null;
}
// file2
var col = new Col();
M(col);
void M<T>(IEnumerable<T> e) where T : IDisposable
{
foreach (var t in e) {
t.Dispose(); // Calling File scoped type member from diff file
}
} The ability to observe the shape of a file scoped type like this goes very much against their design. |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
Allow file-scoped types in explicit interface implementations. Those members are inaccessible without explicit conversion to given interface, thus it is safe to allow them use file-scoped types in their declarations.
Beta Was this translation helpful? Give feedback.
All reactions