CHAPTER 5 UNDERSTANDING ENCAPSULATION
protected
Type members or nested
types
Protected items can be used by the
class which defines it, and any child
class. However, protected items
cannot be accessed from the outside
world using the C# dot operator.
internal
Types or type members
Internal items are accessible only
within the current assembly.
Therefore, if you define a set of
internal types within a .NET class
library, other assemblies are not able
to make use of them.
protected internal
Type members or nested
types
When the protected and internal
keywords are combined on an item,
the item is accessible within the
defining assembly, the defining
class, and by derived classes.
In this chapter, you are only concerned with the public and private keywords. Later chapters will
examine the role of the internal and protected internal modifiers (useful when you build .NET code
libraries) and the protected modifier (useful when you are creating class hierarchies).
The Default Access Modifiers
By default, type members are implicitly private while types are implicitly internal. Thus, the following
class definition is automatically set to internal, while the type’s default constructor is automatically set
to private:
// An internal class with a private default constructor.
class Radio
{
Radio(){}
}
To allow other parts of a program to invoke members of an object, you must mark them as publicly
accessible. As well, if you wish to expose the Radio to external assemblies (again, useful when building
.NET code libraries; see Chapter 14), you will need to add the public modifier.
// A public class with a public default constructor.
public class Radio
{
public Radio(){}
}
Access Modifiers and Nested Types
As mentioned in Table 5-1, the private, protected, and protected internal access modifiers can be
applied to a nested type. Chapter 6 will examine nesting in detail. What you need to know at this point,
however, is that a nested type is a type declared directly within the scope of class or structure. By way of
189