内部班的公开和内部成员? [英] Public and Internal members in an Internal class?
问题描述
好的,所以这可能是一个愚蠢的问题,肯定有一个明显的答案,但是我很好奇我是否错过了这里的任何细微之处。
Ok, so this may be a bit of a silly question, and there's certainly the obvious answer, but I was curious if I've missed any subtleties here.
在内部
类中声明的公共
成员与<$$之间在可见性/可用性方面是否存在差异c $ c>内部成员在内部
类中声明?
Is there any difference in terms of visibility/usability between a public
member declared in an internal
class and an internal
member declared in an internal
class?
ie
internal class Foo
{
public void Bar()
{
}
}
和
internal class Foo
{
internal void Bar()
{
}
}
如果您将该方法声明为 public
以及虚拟
,然后在公共
的派生类中覆盖它,使用此修饰符的原因很明显。但是,这是唯一的情况吗……我还缺少其他东西吗?
If you declared the method as public
and also virtual
, and then overrode it in a derived class that is public
, the reason for using this modifier is clear. However, is this the only situation... am I missing something else?
推荐答案
考虑这种情况:
public interface IBar { void Bar(); }
internal class C : IBar
{
public void Bar() { }
}
此处C.Bar不能标记为内部;这样做是一个错误,因为D.GetBar()的调用者可以访问C.Bar:
Here C.Bar cannot be marked as internal; doing so is an error because C.Bar can be accessed by a caller of D.GetBar():
public class D
{
public static IBar GetBar() { return new C(); }
}
这篇关于内部班的公开和内部成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!