什么这";用C#表示什么意思?

什么这";用C#表示什么意思?,c#,model-view-controller,controller,C#,Model View Controller,Controller,我正在查看一些代码,我看到: public class AccountController : Controller { public AccountController() : this(new xxxxxx... 我知道它是AccountController的构造函数,但其含义是“:this” 谢谢, zalek:构造函数调用另一个重载后,此 考虑这一点: public class Offer { public string offerTitle {

我正在查看一些代码,我看到:

public class AccountController : Controller
{
   public AccountController()   
          : this(new xxxxxx...
我知道它是AccountController的构造函数,但其含义是“:this”

谢谢,
zalek

:构造函数调用另一个重载后,此

考虑这一点:

public class Offer {
    public string offerTitle { get; set; }
    public string offerDescription { get; set; }
    public string offerLocation { get; set; }

    public Offer():this("title") {

    }

    public Offer(string offerTitle) {
        this.offerTitle = offerTitle;
    }
}

如果调用方调用
new Offer()
,那么它将在内部调用另一个构造函数,该构造函数将
offertile
设置为“title”。

它确保调用同一类中的重载构造函数,例如:

class MyClass
{
    public MyClass(object obj) : this()
    {
        Console.WriteLine("world");
    }

    public MyClass()
    {
        Console.WriteLine("Hello");
    }
}
使用参数调用构造函数时的输出:

你好

世界


:此
意味着它将调用父类的主类构造函数(在本例中为控制器)

此关键字允许您从同一类中的另一个构造函数调用一个构造函数。假设类中有两个构造函数,一个带参数,一个不带参数。您可以在不带参数的构造函数上使用
this
关键字将默认值传递给带参数的构造函数,如下所示:

public class AccountController : Controller
{
   public AccountController() : this(0, "")   
   {
       // some code
   }

   public AccountController(int x, string y)   
   {
       // some code
   }
}
还有一个
base
关键字,可以用来调用基类中的构造函数。下面代码中的构造函数将调用
控制器
类的构造函数

public class AccountController : Controller
{
   public AccountController() : base()   
   {
       // some code
   }
}

实际上,输出将是“Helloworld”
控制台。WriteLine
附加了一个行终止符,所以不-不会。哦,甚至是“Helloworld”。别担心这个答案是错的。