构造函数中"this"关键字的功能是什么?

Aar*_*ide 15 .net c# constructor

我刚刚看到来自MSDN的示例代码并且来了:

namespace IListSourceCS
{
    public class Employee : BusinessObjectBase
    {
        private string      _id;
        private string      _name;
        private Decimal     parkingId;

        public Employee() : this(string.Empty, 0) {} // <<--- WHAT IS THIS???
        public Employee(string name) : this(name, 0) {}
Run Code Online (Sandbox Code Playgroud)

Cra*_*nec 15

它使用该签名调用该类中的其他构造函数.它是一种根据其他构造函数实现构造函数的方法. base也可以用来调用基类构造函数.您必须拥有与此匹配的签名构造函数才能使其正常工作.

  • @coder抱歉,我从未使用过MS代码覆盖率,并且没有更详细地了解问题所在,我什至无法真正开始猜测问题是什么 (2认同)

San*_*ath 8

这允许您使用(string,int)参数调用Employee(当前)类的另一个构造函数.

这是一种初始化称为Constructor Chaining的对象的技术