CHAPTER 6 UNDERSTANDING INHERITANCE AND POLYMORPHISM
static void Main(string[] args)
{
// Create and use the public inner class. OK!
OuterClass.PublicInnerClass inner;
inner = new OuterClass.PublicInnerClass();
}
// Compiler Error! Cannot access the private class.
OuterClass.PrivateInnerClass inner2;
inner2 = new OuterClass.PrivateInnerClass();
To make use of this concept within the employees example, assume you have now nested the
BenefitPackage directly within the Employee class type:
partial class Employee
{
public class BenefitPackage
{
// Assume we have other members that represent
// dental/health benefits, and so on.
public double ComputePayDeduction()
{
return 125.0;
}
}
...
}
The nesting process can be as “deep” as you require. For example, assume you want to create an
enumeration named BenefitPackageLevel, which documents the various benefit levels an employee
may choose. To programmatically enforce the tight connection between Employee, BenefitPackage, and
BenefitPackageLevel, you could nest the enumeration as follows:
// Employee nests BenefitPackage.
public partial class Employee
{
// BenefitPackage nests BenefitPackageLevel.
public class BenefitPackage
{
public enum BenefitPackageLevel
{
Standard, Gold, Platinum
}
public double ComputePayDeduction()
{
return 125.0;
}
}
...
}
Because of the nesting relationships, note how you are required to make use of this enumeration:
227