What are the best practices regarding the use and structure of inner classes in C#.
For instance if I have a very large base class and two large inner classes should
I think this is rather subjective, but I would probably split them up in separate code files by making the "host" class partial.
By doing like this, you can get even more overview by editing the project file to make the files group just like designer classes in Windows Forms. I think I've seen a Visual Studio addin that does this automagically for you, but I don't remember where.
EDIT:
After some looking I found the Visual Studio add-in for doing this called VSCommands
Generally inner classes should be private and usable only by the class that contains them. If they inner classes are very large that would suggest they should be their own classes.
Usually when you've got a big inner class it's because the inner class is tightly coupled to it's containing class and needs access to its private methods.
Regarding only how to structure such a beast ...
You can use partial classes to split the main class and the nested classes. When you do so, you're advised to name files appropriately so it's obvious what is going on.
// main class in file Outer.cs
namespace Demo
{
public partial class Outer
{
// Outer class
}
}
// nested class in file Outer.Nested1.cs
namespace Demo
{
public partial class Outer
{
private class Nested1
{
// Nested1 details
}
}
}
In much the same way, you often see (explicit) interfaces in their own file. e.g. Outer.ISomeInterface.cs
rather than the editor default of #region
ing them.
Your projects file structure then starts to look like
/Project/Demo/ISomeInterface.cs /Project/Demo/Outer.cs /Project/Demo/Outer.Nested1.cs /Project/Demo/Outer.ISomeInterface.cs
Typically when we're doing this it's for a variation of the Builder pattern.