简体   繁体   中英

How do I use :base() and :this() correctly?

I've already searched about this issue both on SO and other websites but I haven't managed to find (or come to) a solution for my case.

I have an abstract class called EnteBase , which I use as a base ( duh! ) for other two classes, Regione and Provincia .


EnteBase :

public abstract class EnteBase
{
    public EnteBase ()
        : this( "Sconosciuto", 0 )
    {
    }

    public EnteBase ( string nome )
        : this( nome, 0 )
    {
    }

    public EnteBase ( string nome, int numeroComuni )
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
    }


    private string nome;
    public string Nome
    {
        [...]
    }

    private int numeroComuni;
    public int NumeroComuni
    {
        [...]
    }
}

Regione :

public class Regione : EnteBase
{
    public List<Provincia> Province
    {
        [...]
    }


    public Regione ()
        : base()
    {
        this.Province = new List<Provincia>();
    }

    public Regione ( string nome )
        : this()
    {
    }

    public Regione ( string nome, int numeroComuni )
        : this()
    {
    }


    public void AggiungiProvincia ( Provincia provincia )
    {
        Province.Add( provincia );
    }
}

Provincia :

public class Provincia : EnteBase
{
    private string sigla;

    public string Sigla
    {
        [...]
    }

    public Provincia ()
        : base()
    {
    }

    public Provincia ( string nome )
        : this()
    {
        this.Nome = nome;
    }

    public Provincia ( string nome, int numeroComuni )
        : this()
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
    }

    public Provincia( string nome, int numeroComuni, string sigla)
        : this()
    {
        this.Nome = nome;
        this.NumeroComuni = numeroComuni;
        this.Sigla = sigla;
    }
}

My questions are the following:

  • Is it correct to use :this() in all constructors of the base class except the one with most parameters, with the others pointing towards the latter?
  • Is it correct to use :this() pointing to the base constructor in the classes Provincia and Regione and then assign to the fields from inside the method itself?

My problem rooted from the fact that I wanted to use both :this() and :base() in every method. When I discovered that it was not possible I looked for a solution, but I couldn't find a way to apply what I saw in this question and this one .

PS: in constructors, is it preferred to use this.FieldName or just FieldName ?

Is it correct to use :this() in all constructors of the base class except the one with most parameters, with the others pointing towards the latter?

The only use case is constructor chaining where you minimize your initialization work to one constructor and have all other constructor use this() with parameters that cause that constructor to execute - you are doing exactly this already in your EnteBase class.

An empty base() call is generally useless, since the default base constructor is called by default anyway.

An empty this() call is going the wrong way and generally useless too (unless you have some initialization work that does not depend on the parameters) - you should pass parameters a constructor with more parameters is called, eventually ending up at the one constructor that does all the work.

Is it correct to use :this() pointing to the base constructor in the classes Provincia and Regione and then assign to the fields from inside the method itself?

In C#, a call to :base() is implicit, so it need not be done explicitly.

As a consequence of this, in your examples, you do not need the call to :this() since all this does is call the base parameterless constructor.

Is it correct to use :this() in all constructors of the base class except the one with most parameters, with the others pointing towards the latter?

This is a common way of giving values sensible defaults, yes.

You should change the following Constructor in the subclass from:

public Provincia ( string nome, int numeroComuni )
    : this()
{
    this.Nome = nome;
    this.NumeroComuni = numeroComuni;
}

To:

public Provincia ( string nome, int numeroComuni )
    : base(nome, numeroComuni)
{
}

AND the same in the following Constructor , from:

public Provincia( string nome, int numeroComuni, string sigla)
    : this()
{
    this.Nome = nome;
    this.NumeroComuni = numeroComuni;
    this.Sigla = sigla;
}

To:

public Provincia( string nome, int numeroComuni, string sigla)
    : base(nome, numeroComuni)
{
    this.Sigla = sigla;
}

Eliminating the need to set properties of the superclass in the subclass , as intended by the superclass es implementation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM