The most common is to use the internal type as private and so it functions as an auxiliary type that can only be accessed internally. It is a form of composition with a type that only concerns the type in question. That is, if the internal type will not be used elsewhere, you do not have to expose it to other parts of the code.
You can thus hide a detail of implementation by leaving more detail to the part that is detail. In most cases it is a matter of organization.
An important detail is that the built-in type can access members of the external type, even private ones, without problems if necessary. Everything that was created in a more external scope continues in the same scope. Nesting works identically to methods algorithms.
I would not be able to cite an example where it would be appropriate to create a nested type that is public.
Finding some interesting examples in OS, especially in Eric Lippert's answer where it shows you a way you can create its own hierarchy but prevent others from doing so:
public abstract class BankAccount {
private BankAccount() {} // prevent third-party subclassing.
private sealed class SavingsAccount : BankAccount { ... }
private sealed class ChequingAccount : BankAccount { ... }
public static BankAccount MakeSavingAccount() { ... }
public static BankAccount MakeChequingAccount() { ... }
}
Note that it uses private
and does not leave default . In C # classes, by default are internal
, ie they are visible throughout assembly . It would be almost the same as leaving public
, after all you are creating one class within another but it would be accessible outside of it, so it would not have much advantage.