Builders with this, which means

5

This constructor has this , what does it really mean?

public class HelpController : Controller
{
    private const string ErrorViewName = "Error";

    public HelpController()
        : this(GlobalConfiguration.Configuration)
    {
    }

    public HelpController(HttpConfiguration config)
    {
        Configuration = config;
    }

    public HttpConfiguration Configuration { get; private set; }

    public ActionResult Index()
    {
        ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
        return View(Configuration.Services.GetApiExplorer().ApiDescriptions);
    }

    public ActionResult Api(string apiId)
    {
        if (!String.IsNullOrEmpty(apiId))
        {
            HelpPageApiModel apiModel = Configuration.GetHelpPageApiModel(apiId);
            if (apiModel != null)
            {
                return View(apiModel);
            }
        }

        return View(ErrorViewName);
    }

    public ActionResult ResourceModel(string modelName)
    {
        if (!String.IsNullOrEmpty(modelName))
        {
            ModelDescriptionGenerator modelDescriptionGenerator = Configuration.GetModelDescriptionGenerator();
            ModelDescription modelDescription;
            if (modelDescriptionGenerator.GeneratedModels.TryGetValue(modelName, out modelDescription))
            {
                return View(modelDescription);
            }
        }

        return View(ErrorViewName);
    }
}

View the complete class. Does it inherit from the constructor that is below it? I understood more or less what the colleagues replied, but there is still a question: why this has this: GlobalConfiguration.Configuration . Maybe I'm in doubt in the nomenclature.

    
asked by anonymous 21.05.2017 / 17:36

2 answers

4

You can not call other overloaded constructors inside a constructor, you can only indicate that it should be called, so the compiler puts the call in the most appropriate place.

In case it is calling the other constructor that has a parameter and already passing a default value:

public HelpController(HttpConfiguration config) => Configuration = config;

the parameter config will receive GlobalConfiguration.Configuration 'passed in the other constructor.

Not really, but this constructor would be the same as writing this:

public HelpController() => this.HelpController(GlobalConfiguration.Configuration);

This is called the initializer. This is useful when something needs to be added to the behavior of a constructor that is already present in another constructor (overloading it). In this example, the constructor was created to give a facility, having a constructor that does not need to pass anything that it chooses a default value.

If this is an enumeration the default value could be used for the parameter. Something like this:

public HelpController(HttpConfiguration config = GlobalConfiguration.Configuration)

Just to complement, not to this code, there are cases that : base can be used instead of : this , then it will call the base class constructor to it. Example: public HelpController() : base(passa alguma coisa aqui) . Note that whenever there is inheritance in the class the default constructor of the base class is implicitly called if its code does not explicitly state. So even if you do not see when you make public HelpController() , you're actually doing public HelpController() : base() .

I have already used C # 7 syntax for one-line constructors.

What is a method signature?

    
21.05.2017 / 17:39
3

It's as if this constructor "inherits" another constructor from the same class:

Example:

    public PessoaRepositorio() : this("teste")
    {

    }
    public PessoaRepositorio(string a)
    {

    }
    
21.05.2017 / 17:39