This article provides an in-depth discussion of member and type visibility. It illustrates when and where to make a member either public or private, protected or internal. It even gives you tips to use when building your own libraries. Keep reading to find out more! Before we get started, let’s clarify some terms. Types are the building blocks of an object-oriented language, like C#. An application is a collection of these types interacting together to provide useful information. When we refer to types, we mean classes, structures, interfaces, enumerations, and delegates (these types will be explored in more detail in upcoming articles).
A type, like a class, contains members. Example of members will be methods, fields, constructors, and so on… As stated earlier, an object-oriented application is a collection of types, and a type is a collection of members. When designing a type, a common question arises: should the user of the class (another developer for instance) have access to a specific field in the class, or should that field be privately owned and manipulated by the type? In other words, what is the visibility of each member of the class? The reason this is important is that you do not want the user of your class to change the state of a certain field to an unacceptable value and thus, make that instance of the class useless.