Understanding default access modifiers

时间:2015-06-20 09:39:35

标签: c# class

I am confused about access modifiers, so I thought I would ask a couple of quick questions for clarification:

Is it always the case that in the absence of access modifiers for data members of a class, the default is private, though the class itself is internal?

class A
{
    int x;
}

So, int x is private int x and class A is internal class A?

=========================================

Also, why would the following code not compile?

class A
{
    protected int x;
}

public class B : A
{}

1 个答案:

答案 0 :(得分:2)

作为documentation状态,类和结构默认是内部的,其成员是私有的。

代码不会编译,因为错误消息将声明,您不能从不太容易访问的类继承。在这种情况下,子类将是公共的,而父类是内部的。