Nested Classes
In addition to defining methods and fields within a class, it is possible to define a class within a class. Such classes are called nested classes. You use a nested class when the class makes little sense outside the context of its containing class.
Consider a class that handles the command-line options of a program. Such a class is generally unique to each program, so there is no reason to make a CommandLine class accessible from outside the class that contains Main(). Listing 5.45 demonstrates such a nested class.
LISTING 5.45: Defining a Nested Class
// CommandLine is nested within Program
class Program
{
// Define a nested class for processing the command line.
private class CommandLine
{
public CommandLine(string[] arguments)
{
for(int argumentCounter=0;
argumentCounter<arguments.Length;
argumentCounter++)
{
switch (argumentCounter)
{
case 0:
Action = arguments[0].ToLower();
break;
case 1:
Id = arguments[1];
break;
case 2:
FirstName = arguments[2];
break;
case 3:
LastName = arguments[3];
break;
}
}
}
public string Action;
public string Id;
public string FirstName;
public string LastName;
}
static void Main(string[] args)
{
CommandLine commandLine = new CommandLine(args);
switch (commandLine.Action)
{
case "new":
// Create a new employee
// ...
break;
case "update":
// Update an existing employee's data
// ...
break;
case "delete":
// Remove an existing employee's file.
// ...
break;
default:
Console.WriteLine(
"Employee.exe " +
"new|update|delete <id> [firstname] [lastname]");
break;
}
}
}
The nested class in this example is Program.CommandLine. As with all class members, no containing class identifier is needed from inside the containing class, so you can simply refer to it as CommandLine.
One unique characteristic of nested classes is the ability to specify private as an access modifier for the class itself. Because the purpose of this class is to parse the command line and place each argument into a separate field, Program.CommandLine is relevant only to the Program class in this application. The use of the private access modifier defines the intended accessibility of the class and prevents access from outside the class. You can do this only if the class is nested.
The this member within a nested class refers to an instance of the nested class, not the containing class. One way for a nested class to access an instance of the containing class is if the containing class instance is explicitly passed, such as via a constructor or method parameter.
Another interesting characteristic of nested classes is that they can access any member on the containing class, including private members. The converse is not true, however: It is not possible for the containing class to access a private member of the nested class.
Nested classes are rare. They should not be defined if they are likely to be referenced outside the containing type. Furthermore, treat public nested classes with suspicion; they indicate potentially poor code that is likely to be confusing and hard to discover.