Nested Classes

Microsoft .NET Framework, ASP.NET, Visual C# (CSharp, C Sharp, C-Sharp) Developer Training, Visual Studio


Jump to: navigation, search
Exam Prep. Guides
Exam 70-536 Study Guide

1. Types and collections

2. Process, threading,…
3. Embedding features
4. Serialization, I/O
5. .NET Security
6. Interop., reflection,…
7. Global., drawing, text

edit

Contents


A nested class is one that is created inside another class.

Why ?

A good question that is not always immediately answerable. There are several reasons to have inner classes. But the two that jump to mind are these:

  • Organizing code into real world situations where there is a special relationship between two objects.
  • Hiding a class within another class so that you do not want the inner class to be used from outside of the class it is created within.


Polymorphism

Suppose you want to model a car. Some objects only make sense within a whole car and not on their own. We can say that the car is composed of other objects. Polymorphism uses two terms to describe this, aggregation and composition. These relationships form a whole-part relationship that you can use to decompose objects into more manageable entities.

  • Any object that can exist and be used independently of the relationship uses aggregation.
  • Any object that has no meaning outside of the relationship uses composition.

An example

For example, a class named Car would have an engine. Objects that fall into this relationship use of the term "is a part of". "An engine is a part of a car". In UML, an object relationship that is formed by aggregation is drawn using an empty diamond. An object relationship that is formed using composition is drawn using a filled diamond.

The following UML diagram illustrates the concepts of aggregation and composition.


Image:nestedcar.jpg


In the diagram above, the battery and the engine have no meaning outside of the car, as the car cannot work without either of them, so the relationship is formed using composition. However, a car can work without doors, so the relationship is formed using aggregation.

From the previous diagram, we can create a possible small code sample that shows the two forms of relationship. Objects that use composition are created as inner classes.

namespace MyCars
{
  public class Car
  {
    // Aggregation uses instances of objects created outside of
    // this class
    protected Door FrontRight;
    protected Door FrontLeft;
    protected Door RearRight;
    protected Door RearLeft;
 
    // inner classes used to create objects that are intrinsically
    // linked to the class Car
    protected class Engine
    {
      public int horsePower;
    }
 
    protected class Battery
    {
      public int voltage;
    }
 
    // Composition uses instances of objects that are created as
    // part of this object
    protected Engine theEngine;
    protected Battery theBattery;
 
    public Car()
    {
      theEngine = new Engine();
      theBattery = new Battery();
    }
  }
 
  public class Door
  {
    public int position;
  }
}

By giving each of the members protected access modifiers, each class that inherits from the class Car has access to each of the members of the class Car.

namespace MyCars
{
  // Inherit from class Car
  public class FordCapri : Car
  {
    public FordCapri()
    {
      theEngine.horsePower = 2000;
    }
  }
}

Declaration and use

A nested class is declared in the same manner as a normal class declaration. The difference is that a nested class has access to all of the available modifiers.

The this keyword reference in the inner class only holds a reference to the inner class. Data members of the outer class are not accessible using the this reference in the inner class. If this is needed, pass a reference of the outer class into the constructor of the inner class.

static members of the outer class are available in the inner class irrespective of the accessibility level.


Modifiers

Any of the following modifiers can be used with a nested class including:

abstract - the class is created solely for the purpose of inheritance. You cannot create an instance of an abstract class.

sealed - the class cannot be inherited from.

static – the class can only contain static members.

unsafe - allows for unsafe constructs such as pointers. Requires the unsafe compiler option.

public - any item in the current assembly or any assembly that references it, can access this class.

protected - access is limited to the containing class or types derived from the containing class.

internal - any item in the current assembly can access this class.

protected internal - access is limited to the current assembly or types derived from the containing class.

private - access is limited to the containing class.


MSDN references


Previous_Page_.gif Next_Page_.gif


Personal tools