Home

C# Topics: Nesting a Class

     

Introduction

A class can be created inside of another class. A class created inside of another is referred to as nested. To nest a class:

  • Click inside an existing class and type the necessary code for the new class, starting with the class keyword followed by a name and {}
  • Select the whole class. Right-click the selection and click Surround With... In the list, double-click class
 

Here is an example of a class called Inside that is nested in a class called Outside:

public class Outside
{
    public class Inside
    {
    }
}

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 if you judge it necessary. Just as you would manage any other class so can you exercise control on a nested class. For example, 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. They are two different classes and they can be used separately as you judge it necessary. 

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:

using System;

public class Outside
{
    public class Inside
    {
	public Inside()
	{
	    Console.WriteLine(" -= Inside =-");
	}
    }

    public Outside()
    {
	Console.WriteLine(" =- Outside -=");
    }
}

public class Exercise
{
    static int Main()
    {
	Outside recto = new Outside();
	Outside.Inside ins = new Outside.Inside();

	return 0;
    }
}

This would produce:

=- Outside -=
 -= Inside =-

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:

using System;

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

	public Inside()
	{
		Console.WriteLine(" -= Insider =-");
		inMessage = "Sitting inside while it's raining";
	}

	public static void Show()
	{
		Console.WriteLine("Show me the wonderful world of C# Programming");
	}
    }

    public Outside()
    {
	Console.WriteLine(" =- The Parent -=");
    }

    public void Display()
    {
	Console.WriteLine(Inside.InMessage);
	Inside.Show();
    }
}

class Exercise
{
    static int Main()
    {
	Outside recto = new Outside();
	Outside.Inside ins = new Outside.Inside();

	Recto.Display();
	return 0;
    }
}

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:

using System;

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

		public Inside()
		{
			Console.WriteLine(" -= Insider =-");
			InMessage = "Sitting inside while it's raining";
		}

		public static void Show()
		{
		Console.WriteLine("Show me the wonderful world of C# Programming");
		}

		public void FieldFromOutside()
		{
			Console.WriteLine(Outside.OutMessage);
		}
	}

	private static string OutMessage;

	public Outside()
	{
		Console.WriteLine(" =- The Parent -=");
		OutMessage = "Standing outside! It's cold and raining!!";
	}

	public void Display()
	{
		Console.WriteLine(Inside.InMessage);
		Inside.Show();
	}
}

public class Exercise
{
    static int Main()
    {
	Outside recto = new Outside();
	Outside.Inside Ins = new Outside.Inside();

	Recto.Display();
	Console.WriteLine();
	Ins.FieldFromOutside();
	return 0;
    }
}

This would produce:

=- The Parent -=
 -= Insider =-
Sitting inside while it's raining
Show me the wonderful world of C# Programming

Standing outside! It's cold and raining!!

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. Here is an example:

using System;

public class Outside
{
	// A member of the nesting class
	private string OutMessage;

	// The nested class
	public class Inside
	{
		// A field in the nested class
		public string InMessage;

		// A constructor of the nested class
		public Inside()
		{
			Console.WriteLine(" -= Insider =-");
			this.InMessage = "Sitting inside while it's raining";
		}

		// A method of the nested class
		public void Show()
		{
			// Declare a variable to access the nesting class
			Outside outsider = new Outside();
			Console.WriteLine(outsider.OutMessage);
		}
	} // End of the nested class

	// A constructor of the nesting class
	public Outside()
	{
		this.OutMessage = "Standing outside! It's cold and raining!!";

		Console.WriteLine(" =- The Parent -=");
	}

	// A method of the nesting class
	public void Display()
	{
		Console.WriteLine(insider.InMessage);
	}

	// Declare a variable to access the nested class
	Inside insider = new Inside();
}

public class Exercise
{
    static int Main()
    {
	Outside recto = new Outside();
	Outside.Inside Ins = new Outside.Inside();

	Ins.Show();
	Recto.Display();
	return 0;
    }
}

This would produce:

-= Insider =-
 =- The Parent -=
 -= Insider =-
 -= Insider =-
 =- The Parent -=
Standing outside! It's cold and raining!!
Sitting inside while it's raining
 
 

Home Copyright © 2010-2011 FunctionX