The this
keyword allows you to call one constructor from another constructor in the same class. Let's say you have two constructors in your class, one with parameters and one without parameters. You can use the this
keyword on the constructor with no parameter to pass default values to the constructor with parameters, like this:
public class AccountController : Controller
{
public AccountController() : this(0, "")
{
// some code
}
public AccountController(int x, string y)
{
// some code
}
}
There's also a base
keyword that you can use to call a constructor in the base class. The constructor in the following code will call the constructor of the Controller
class.
public class AccountController : Controller
{
public AccountController() : base()
{
// some code
}
}