Modifiers
You have already encountered quite a number
of so-called modifiers - keywords that can be applied to a type or
to a member. Modifiers can indicate the visibility of a method,
such as public or private, or the nature of an item, such as whether a
method is virtual or abstract. C# has a number of modifiers, and at this
point it’s worth taking a minute to provide the complete list.
Visibility Modifiers
Visibility modifiers indicate which other
code items can view an item.
Note that type definitions can be internal or
public, depending on whether you want the type to be visible
outside its containing assembly.
You cannot define types as protected, private, or
protected internal, because these visibility levels would be
meaningless for a type contained in a namespace. Hence these
visibilities can only be applied to members. However, you can
define nested types (that is, types contained within other types)
with these visibilities, because in this case the type also has the
status of a member. Hence, the following code is correct:
If you have a nested type, the inner type is
always able to see all members of the outer type. Therefore, with
the preceding code, any code inside InnerClass always has access to all members of
OuterClass, even where those members are
private.
Other Modifiers
The modifiers in the following table can be
applied to members of types and have various uses. A few of these
modifiers also make sense when applied to types.
Of these, internal
and protected internal are the ones that
are new to C# and the .NET Framework. internal acts in much the same way as public, but access is confined to other code in the
same assembly - that is, code that is being compiled at the same
time in the same program. You can use internal to ensure that all the other classes that
you are writing have access to a particular member, while at the
same time hiding it from other code written by other organizations.
protected internal combines protected
and internal, but in an OR sense, not an AND sense. A protected
internal member can be seen by any code in the same assembly. It
can also be seen by any derived classes, even those in other
assemblies.