Controlling Inheritance
Article Index
Controlling Inheritance
Access modifiers
Sealed
Nested classes
Extension methods

Banner

Nested classes

A nested class is another way to control the visibility and access to a class. To create a nested class you simply declare the class in the usual way but within the definition of another class. For example:

public  class MyClass1
{
public class MyNestedClass
{
public void MyMethod2()
{
Console.WriteLine("Method2");
}
}
}

MyNestedClass is declared within MyClass1 and so is nested within MyClass1.

Compare this to the simpler idea of creating an instance of a class as a member of a class, e.g:

public  class MyClass1
{
public MyClass2 myobj=new MyClass2();
etc.

This isn't a nested class just an instance of MyClass2 that is a member of MyClass1.

Notice that both classes are declared as public and this isn't good practice but let's follow the example a little further to see how it works.

If we also declare a method within MyClass1 it can make use of the nested class in the usual way:

public  void MyMethod1()
{
MyNestedClass myObj1 =
new MyNestedClass();
myObj1.MyMethod2();
Console.WriteLine("Method1");
}
}

Any code within MyClass1 can create and use objects of type MyNestedClass. If you move outside of MyClass1 then your code can create objects of type MyClass1 which in turn use objects of MyNestedClass. For example:

MyClass1 myob2 = new MyClass1();
myob2.MyMethod1();

However, MyNestedClass isn't directly accessible from code outside of MyClass1. As the nested class is public it can be accessed using a qualified name. For example, to create an instance of MyNestedClass outside of MyClass1 you would use:

MyClass1.MyNestedClass myobj3 = 
new MyClass1.MyNestedClass();

and you could then access the object's methods and properties in the usual way:

myobj3.MyMethod2();

This use of nested classes to create types with qualified names isn't usual and most programmers, and the Microsoft recommendations, say that this isn't a good use. Also notice that nested classes are just a special case of nested types where a type, a struct say, is declared within a class.

In most cases nested classes are used as a way of hiding the implementation of a class. Normally classes cannot use the private or protected type modifier but nested classes can. What this means is that if you declare the nested class to be private or protected it is no longer accessible to code outside of the containing class, even if you use a qualified name. However it is accessible from within the containing class. In short the whole of a nested class is private or protected according to its declaration.

A nested class also has access to all of the members of the outer containing class but a reference to the instance of the class isn't provided automatically. That is, "this" within the containing class refers to the instance of the containing class and "this" within the nested class refers to the instance of the nested class not the containing class.

To allow the nested class access to the containing class you have to pass it a reference to the instance. For example:

public  class MyClass1
{
protected class MyNestedClass
{
private MyClass1 that;
public MyNestedClass(
MyClass1 outerthis)
{
that= outerthis;
}

Now when the nested class is created the container passes a reference to its current instance:

MyNestedClass myObj1 = 
new MyNestedClass(this);

The inner class can use the variable name "that" to access members of the container class including private and protected members. This is the C# equivalent of the Java inner class where the instance of the nested class is bound to an instance of the containing class.

Banner

Clearly the intended use for a nested class is to allow the creation of classes, and types in general, that are only to be used within another class. Hence the reason why public nested classes are generally thought to be bad practice.

<ASIN:0470261293>

<ASIN:0596527578>



 
 

   
RSS feed of all content
I Programmer - full contents
Copyright © 2014 i-programmer.info. All Rights Reserved.
Joomla! is Free Software released under the GNU/GPL License.