在接口 c# 8 中使用静态、内部和受保护的访问修饰符
Using static, internal and protected access modifiers in interfaces c# 8
C# 8 现在支持接口成员的访问修饰符,它的用法让我很困惑。举个例子
public interface IFoobar
{ // these members are all valid
protected string Protected { get; set; }
internal string Internal { get; set; }
static string Static { get; set; }
}
public class Foobar : IFoobar // <-- error, Internal and Protected members not implemented
{
protected string Protected { get; set; }
internal string Internal { get; set; }
static string Static { get; set; } // only this one implements IFoobar
}
我的期望是上面的 Foobar
会完全实现 IFoobar
。但是只有Static
是这样,其他的不是。
有人可以吗
- 解释为什么它们的行为不同(并且也不同于 pre-c#8 接口成员)
- 给我一个像这样的界面中三个修饰符的每一个的用例?
谢谢
[编辑]
我知道使用显式接口实现将实现成员,但对于 c#8 之前的接口成员来说,这并不是严格意义上的唯一方法。为什么这与新成员不同?
I am aware that using explicit interface implementation will implement the members, but for pre-c#8 interface members that wasn't strictly the only way. Why is this different with the new members?
It appears this is by design。以下是相关文字:
Implicitly implementing non-public interface members
Would we allow non-public interface members to be implemented implicitly? If so, what is required of the accessibility of the implementing method? Some options:
- Must be public
- Must be the exact same accessibility
- Must be at least as accessible
Conclusion
For now, let's simply not allow it. Only public interface members can be implicitly implemented (and only by public members). We can relax as we think through it.
显然,带有访问修饰符的接口方法不会完全按照与以前版本的接口成员相同的规则运行,因为它们只能是 public.
至于为什么会这样,那是设计师的问题了。 LDM 的措辞也没有让人觉得这是一成不变的。因此,可能 隐式实现的修改访问权限的成员将来会被允许。
目前,实现此接口的方法是显式实现,如下所示:
public interface IFoobar
{ // these members are all valid
protected string Protected { get; set; }
internal string Internal { get; set; }
static string Static { get; set; }
}
public class Foobar : IFoobar
{
string IFoobar.Protected {get;set;}
string IFoobar.Internal {get;set;}
}
C# 8 现在支持接口成员的访问修饰符,它的用法让我很困惑。举个例子
public interface IFoobar
{ // these members are all valid
protected string Protected { get; set; }
internal string Internal { get; set; }
static string Static { get; set; }
}
public class Foobar : IFoobar // <-- error, Internal and Protected members not implemented
{
protected string Protected { get; set; }
internal string Internal { get; set; }
static string Static { get; set; } // only this one implements IFoobar
}
我的期望是上面的 Foobar
会完全实现 IFoobar
。但是只有Static
是这样,其他的不是。
有人可以吗
- 解释为什么它们的行为不同(并且也不同于 pre-c#8 接口成员)
- 给我一个像这样的界面中三个修饰符的每一个的用例?
谢谢
[编辑]
我知道使用显式接口实现将实现成员,但对于 c#8 之前的接口成员来说,这并不是严格意义上的唯一方法。为什么这与新成员不同?
I am aware that using explicit interface implementation will implement the members, but for pre-c#8 interface members that wasn't strictly the only way. Why is this different with the new members?
It appears this is by design。以下是相关文字:
Implicitly implementing non-public interface members
Would we allow non-public interface members to be implemented implicitly? If so, what is required of the accessibility of the implementing method? Some options:
- Must be public
- Must be the exact same accessibility
- Must be at least as accessible
Conclusion
For now, let's simply not allow it. Only public interface members can be implicitly implemented (and only by public members). We can relax as we think through it.
显然,带有访问修饰符的接口方法不会完全按照与以前版本的接口成员相同的规则运行,因为它们只能是 public.
至于为什么会这样,那是设计师的问题了。 LDM 的措辞也没有让人觉得这是一成不变的。因此,可能 隐式实现的修改访问权限的成员将来会被允许。
目前,实现此接口的方法是显式实现,如下所示:
public interface IFoobar
{ // these members are all valid
protected string Protected { get; set; }
internal string Internal { get; set; }
static string Static { get; set; }
}
public class Foobar : IFoobar
{
string IFoobar.Protected {get;set;}
string IFoobar.Internal {get;set;}
}