Nested Types

You can define types inside of other types, known as nested types. Nested types have access to the members of their enclosing types as well as to the members of their enclosing type's base class (subject to the normal accessibility conditions, as will be discussed shortly). Listing 2.11 demonstrates their use. This program defines three different object types: Outer, Middle, and Inner. Inner contains the entry point and accesses the enumerations defined in its enclosing types.

Listing 2.11. Nested types
using System;

namespace NestedTypes
{
  class Outer
  {
    private enum Day {Sunday, Monday, Tuesday,
        Wednesday, Thursday, Friday, Saturday};
    class Middle
    {
      private enum Month {January = 1, February,
              March, April, May, June, July, August,
              September, October, November, December};
      class Inner
      {
        static void Main(string[] args)
        {
          Day d = Day.Sunday;
          Month m = Month.September;
          Console.WriteLine(
             "Father's day is the first "
             + d + " in " + m);
        }
      }
    }
  }
}

Running this program generates the following output:[8]

[8] Father's Day is the first Sunday in September in Australia.

Father's day is the first Sunday in September 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset