为什么不允许在内部类中实现内部接口(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 typeT
within a programP
is defined as follows (noting thatM
itself may possibly be a type):
- If the declared accessibility of
M
ispublic
, the accessibility domain ofM
is the accessibility domain ofT
.
因此,成员的可访问性与声明为 internal
时等效。
关于c# - 在 C# 的内部类中实现内部接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47542754/