Why must methods implementing internal interfaces be public

后端 未结 2 822
情话喂你
情话喂你 2021-01-13 13:06

I am developing an internal class that implements an internal interface. Can anyone explain why I cannot declare my method as internal, why I am getting the following error

2条回答
  •  北荒
    北荒 (楼主)
    2021-01-13 13:28

    Simply put: because that's the way the language designers designed it. Even in internal interfaces, the methods are implicitly public. It does make things simple, but it's a pain in other ways.

    If you want a public class where you want to "hide" the use of an internal interface, you could use explicit interface implementation - although that has other drawbacks.

    Of course, if your class is internal then it doesn't matter that the methods are public anyway - other assemblies aren't going to be able to call the methods because they can't see the type.

    I definitely agree that C# (or .NET in general) hasn't been designed as carefully as it might be around internal interfaces.

    In terms of exactly why you're getting an error message - section 13.4.4 of the C# 4 spec (interface mapping) is the reason. Implementations are only found for nonstatic public members and explicit interface member implementations - and if there are any unimplemented members in the interface, an error occurs.

提交回复
热议问题