Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement an internal interface in an internal class in C#

Why am I not allowed to implement an internal interface in an internal class? Is there a specific reason for this restriction or is it just a language design decision?

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();
    }
}
like image 907
Vahid Avatar asked Dec 20 '25 15:12

Vahid


1 Answers

§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.

like image 56
adjan Avatar answered Dec 24 '25 11:12

adjan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!