private int v;
public int w;
protected int x;
internal int y;
protected internal int z;
}
The A
class has four data members, each with a different access modifiers. The fifth data member, z
, has a combination of two access modifiers — protected
and internal
. To see the difference between all these different modifiers, create an instance of A
and observe the members displayed by IntelliSense.
Figure 6-5 shows that only the variables w
, y
, and z
are accessible.

Figure 6-5
At this moment, you can conclude that:
□ The private
keyword indicates that the member is not visible outside the type (class).
□ The public
keyword indicates that the member is visible outside the type (class).
□ The protected
keyword indicates that the member is not visible outside the type (class).
□ The internal
keyword indicates that the member is visible outside the type (class).
□ The protected internal
keyword combination indicates that the member is visible outside the type (class).
Now define a second class, B
, that inherits from class A
:
public class B : A {
public void Method() {}
}
Try to access the class A
variables from within Method()
. In Figure 6-6, IntelliSense shows the variables that are accessible.

Figure 6-6
As you can see, member x
is now visible (in addition to w
, y
, and z
), so you can conclude that:
□ The private
keyword indicates that the member is not visible outside the type (class) or to any derived classes.
□ The public
keyword indicates that the member is visible outside the type (class) and to all derived classes.
□ The protected
keyword indicates that the member is not visible outside the type (class) but is visible to any derived classes.
□ The internal
keyword indicates that the member is visible outside the type (class) as well as to all derived classes.
□ The protected internal
keyword combination indicates that the item is visible outside the type (class) as well as to all derived classes.
From these conclusions, the difference among private
, public
, and protected
is obvious. However, there is no conclusive difference between internal
and protected internal
. The internal
access modifier indicates that the member is only visible within its containing assembly. The protected internal
keyword combination indicates that the member is visible to any code within its containing assembly as well as derived types.
Besides applying the access modifiers to data members, you can also use them on type definitions. However, you can only use the private
and public
access modifiers on class definitions.
Inheritance and Constructors
Consider the following BaseClass
definition consisting of one default constructor:
public class BaseClass {
}
Anther class, DerivedClass
inheriting from the BaseClass
, also has a default constructor:
public class DerivedClass : BaseClass {
//---default constructor---
public DerivedClass() {
Console.WriteLine('Constructor in DerivedClass');
}
}
So when an object of DerivedClass
is instantiated, which constructor will be invoked first? The following statement shows that the constructor in the base class will be invoked before the constructor in the current class will be invoked:
DerivedClass dc = new DerivedClass();
The outputs are:
Constructor in BaseClass
Constructor in DerivedClass
What happens if there is no default constructor in the base class, but perhaps a parameterized constructor like the following?
public class BaseClass {
Console.WriteLine('Constructor in BaseClass');
}
}
In that case, the compiler will complain that BaseClass
does not contain a default constructor.
Remember that if a base class contains constructors, one of them must be a default constructor.
Calling Base Class Constructors