Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 继承自:this()意味着什么?_C# - Fatal编程技术网

C# 继承自:this()意味着什么?

C# 继承自:this()意味着什么?,c#,C#,我正在看一门C#课程,其中一部分代码吸引了我的眼球,教程中没有对此进行解释 Public Product(int productId, string productName, string description) : this() { this.ProductId = productId; this.ProductName = productName; this.description = description; } :this()在构造函数中是什么意思?该:指示初

我正在看一门C#课程,其中一部分代码吸引了我的眼球,教程中没有对此进行解释

Public Product(int productId, string productName, string description) : this()
{
    this.ProductId = productId;
    this.ProductName = productName;
    this.description = description;
}

:this()
在构造函数中是什么意思?

指示初始值设定项列表的开始
this()
调用默认构造函数
Product()
,该构造函数可以隐式或显式定义

如果定义了一个构造函数,它接受一个或多个参数,那么就没有隐式定义的默认构造函数

只有在同一类中显式定义了默认构造函数
Product()
时,示例构造函数
Product(int-productId,string-productName,string-description)
才会编译,因为在这种情况下默认构造函数
Product()
不是隐式定义的


在执行
产品(int-productId,string-productName,string-description)
主体中的代码之前,将调用显式定义的构造函数
Product()
。如果
Product()
为空(并且没有初始值设定项列表),它将不会执行任何操作。

这不是继承,它的意思是“首先,调用不需要参数的构造函数(然后继续使用此构造函数)。如果我理解正确,Hi Stonar:this()应该自动创建“this.ProductId=ProductId;”;。。。。“part?@AndrásCzeitner No,:这将执行无参数产品构造函数中编写的任何操作。因此,如果我在它为空的情况下得到正确的结果,它实际上不会执行任何操作,但如果我在this()中定义某个内容,它将在正常构造函数之前运行,对吗?@AndrásCzeitner,我已编辑了答案。请参见上文。