C# Topics: Nesting a Class
A class can be created inside of another class. A class created inside of another is referred to as nested. To nest a class:
Introduction#
It is possible to define a class within the scope of another class, which is known as a nested class. To perform nesting:
- 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
{
}
}You have the flexibility to nest multiple classes inside each other as needed. Inside Outside
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 =-There is no special programmatic link or inherited access between a nested class and its container by default.
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 rainingHome Copyright © 2010-2011 C# Key

