c# - 在 C# 的内部类中实现内部接口(interface)

标签 c# .net class interface internals

为什么不允许在内部类中实现内部接口(interface)?这种限制是否有特定原因,或者仅仅是语言设计决定?

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();
    }
}

最佳答案

C# 6.0 语言规范的§3.5.6 指出:

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

所以你“理论上”拥有的是

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

但这不是问题,因为(§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.

因此,成员的可访问性与声明为 internal 时等效。

关于c# - 在 C# 的内部类中实现内部接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542754/

相关文章:

c# - 使第三方库实现接口(interface)的策略?

c# - MonoTouch - LLVM 编译的 App 在 5.3.x 的 System.Collections.Generic.Dictionary 中挂起?

c# - Windows 窗体应用程序中的图像质量差(无 AA)

java - 访问对象(动态选择的)子类的函数

c# - 在javascript中获取base64到silverlight图像

c# - 将 Action 注入(inject) UserControl

.net - Lucene .Net SetBoost on Field 不影响结果

c# - 从 C# 到 C++ 的转换

ios - 将您的自定义类命名为与 Swift/iOS 中的文件名相同的命名约定是否是一个好的命名约定

class - 类的 Pythonic : use of __dict__ in the function self. __init__