What are nested types?

13

I know that C # supports nested types, that is, it is possible for me to declare one class within another. For example:

public class A
{
    // Propriedades e métodos da classe A

    public class B
    {
         // Propriedades e métodos da classe B
    }
}

I just never understood what this is for. What can this construction be used for and what are the benefits?

    
asked by anonymous 22.02.2015 / 20:58

2 answers

11

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.

    
22.02.2015 / 21:13
11

Basically, scope limitation, but I will make a small change so that the utility is better understood:

public class A
{
    // Propriedades e métodos da classe A

    class B
    {
         // Propriedades e métodos da classe B
    }
}

Notice that I removed the public from B . This is because class nesting is only really useful when B is protected within the A statement.

And why this?

Because your application may only use the class internally. A great example would be using the Factory design pattern along with inheritance and immutable classes. Let's use a concrete example:

public abstract class ContaBancaria
{
    private ContaBancaria() { }
    private sealed class ContaPoupanca : ContaBancaria { ... }
    private sealed class ContaCorrente : ContaBancaria { ... }
    public static ContaBancaria CriarContaCorrente() { ... }
    public static ContaBancaria CriarContaPoupanca() { ... }
}

In this case, I guarantee that what will be returned will either be ContaCorrente or ContaPoupanca , the two derivatives being ContaBancaria , without being able to extend them (which could be a security problem) and without the ability to exploit constructors, if that class is part of an API, for example.

    
22.02.2015 / 21:13