Home

Class Nesting

 

Description

A class can be created inside of another class. A class created inside of another is referred to as nested. To nest a class, simply create it as you would any other. Here is an example of a class called Inside that is nested in a class called Outside:

<script runat="server">

public class Outside
{
    public class Inside
    {
    }
}

</script>

In the same way, you can nest as many classes as you wish in another class and you can nest as many classes inside of other nested classes. You can declare all necessary fields, properties, or methods in the nested class or in the nesting class.

When you create one class inside of another, there is no special programmatic relationship between both classes:  just because a class is nested does not mean that the nested class has immediate access to the members of the nesting class.

The name of a nested class is not "visible" outside of the nesting class. To access a nested class outside of the nesting class, you must qualify the name of the nested class anywhere you want to use it. For example, if you want to declare an Inside variable somewhere in the program but outside of Outside, you must qualify its name. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public class Outside
{
    public class Inside
    {
	public Inside()
	{
	    
	}
    }

    public Outside()
    {
	
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Outside Recto = new Outside();
    Outside.Inside Ins = new Outside.Inside(); 
%>
</body>
</html>

Because there is no programmatically privileged relationship between a nested class and its "container" class, if you want to access the nested class in the nesting class, you can use its static members. In other words, if you want, you can declare static all members of the nested class that you want to access in the nesting class. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public class Outside
{
    public class Inside
    {
	public static string InMessage;

	public Inside()
	{
	}

	public static void Show()
	{
	}
    }

    public Outside()
    {
    }

    public void Display()
    {
	Inside.Show();
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Outside Recto = new Outside();
    Outside.Inside Ins = new Outside.Inside();

    Recto.Display(); 
%>
</body>
</html>

In the same way, if you want to access the nesting class in the nested class, you can go through the static members of the nesting class. To do this, you can declare static all members of the nesting class that you want to access in the nested class. Here is an example:

<%@ Page Language="C#" %>
<html>
<head>

<script runat="server">

public class Outside
{
    public class Inside
    {
	public static string InMessage;

	public Inside()
	{
		InMessage = "Sitting inside while it's raining";
	}

	public static void Show()
	{
	}

	public void FieldFromOutside()
	{
	}
    }

    private static string OutMessage;

    public Outside()
    {
	OutMessage = "Standing outside! It's cold and raining!!";
    }

    public void Display()
    {
	Inside.Show();
    }
}

</script>

<title>Exercise</title>
</head>
<body>

<%
    Outside Recto = new Outside();
    Outside.Inside Ins = new Outside.Inside();

    Recto.Display();
    
    Ins.FieldFromOutside();
%>
</body>
</html>

Instead of static members, if you want to access members of a nested class in the nesting class, you can first declare a variable of the nested class in the nesting class. In the same way, if you want to access members of a nesting class in the nested class, you can first declare a variable of the nesting class in the nested class. 

 
 
 
     
 

Home Copyright © 2009-2015, FunctionX, Inc.