简体   繁体   中英

Implement an internal interface in an internal class in C#

Why can't I implement an internal interface in an internal class?

internal interface IDefinition
{
    string GetValueAsString(string property);
}

internal sealed class DefinitionArray : IDefinition
{
    internal string GetValueAsString(string property)
    {
        return m_definitionRows
            .Select(o => o.GetValueAsString(property))
            .FirstOrDefault();
    }
}

§3.5.6 of the C# 6.0 Language Specification states:

Interface members implicitly have public declared accessibility. No access modifiers are allowed on interface member declarations.

So what you 'theoretically' have is

internal interface IDefinition
{
    public string GetValueAsString(string property);
}

But this is not a problem, since (§3.5.2):

The accessibility domain of a nested member M declared in a type T within a program P is defined as follows (noting that M itself may possibly be a type):

  • If the declared accessibility of M is public , the accessibility domain of M is the accessibility domain of T .

So the accessibility of the member is equivalent as it would have been declared as internal .

The members that make up the Interface's implementation must be public. Even if they are public since the class is internal it's only available to the proper assembly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM