C# `这是一个类构造函数

C# `这是一个类构造函数,c#,.net,oop,C#,.net,Oop,我现在有一个类,我对它的构造函数有点困惑 public class BarListTracker : GotTickIndicator { public BarListTracker(BarInterval interval) : this(new BarInterval[] { interval }) { } } 语句this(新的BarInterval[]{interval})意味着什么?这是。实际上,在执行构造函数的内容之前,您正在调用另一个构造函数 public clas

我现在有一个类,我对它的构造函数有点困惑

 public class BarListTracker : GotTickIndicator
 {
    public BarListTracker(BarInterval interval) : this(new BarInterval[] { interval }) { }
 }
语句
this(新的BarInterval[]{interval})
意味着什么?

这是。实际上,在执行构造函数的内容之前,您正在调用另一个构造函数

public class Foo
{
    public Foo()
        : this("Hello")
    {
        Console.Write(" World!");
    }

    public Foo(string text)
    {
        Console.Write(text);
    }
}

new Foo(); //outputs "Hello World!"
因此,在
BarListTracker
中的某个地方,应该有另一个构造函数,它接受
BarInterval[]
数组或
IEnumerable
如下所示:

public class BarListTracker : GotTickIndicator
{
    public BarListTracker(BarInterval interval) 
        : this(new BarInterval[] { interval }) 
    { 
        //specific initialization for this constructor
    }

    public BarListTracker(BarInterval[] intervals) 
    { 
        //shared initialization logic for both constructors
    }
}
public BarListTracker(BarInterval[] intervals)
它将执行正文
BarListTracker(BarInterval[])
,然后执行正文
BarListTracker(BarInterval)

这通常用于减少代码重复。如果您的
BarListTracker
有一些初始化代码,那么在一个地方编写它并与构造函数共享该逻辑比为每个构造函数重写它更有意义


此外,它还允许您使用基本表达式传入或修改输入参数。因此在本例中,在调用
BarListTracker(BarInterval[])
构造函数的同时,它将单个
BarInterval interval
对象包装为一个数组以匹配签名。这可能只是一个方便的重载,为程序员提供了一个更简单的API,程序员通常只有一个
BarInterval
来构造跟踪器。

这意味着调用另一个
BarListTracker
的构造函数,该构造函数接受一组
BarInterval
对象,传入包含传入此构造函数的对象的数组。是它打电话的

var tracker = new BarListTracker(interval);
相当于:

var tracker = new BarListTracker(new BarInterval[] { interval });

调用该类中的另一个构造函数,如下所示:

public class BarListTracker : GotTickIndicator
{
    public BarListTracker(BarInterval interval) 
        : this(new BarInterval[] { interval }) 
    { 
        //specific initialization for this constructor
    }

    public BarListTracker(BarInterval[] intervals) 
    { 
        //shared initialization logic for both constructors
    }
}
public BarListTracker(BarInterval[] intervals)

这表示当调用该构造函数时,它们正在基于或调用类中的另一个构造函数,并将该值作为
BarInterval
数组传递。在本例中,它不是一个基类,因为否则它会说
:base(…)
,它是在同一个类中定义的另一个构造函数

这是非常常见的,因为您希望以多种不同的方式访问类,在本例中,他们似乎希望能够在不在代码中设置数组的情况下发送单个对象

但是,他们可以做的一件事就是更改另一个构造函数,用
调用的构造函数:this
为:

public BarListTracker(params BarInterval[] interval)
他们甚至不需要第二个构造函数。这是一种更清洁的解决方案,在任何地方都能产生相同的结果。另一个构造函数仍然会得到一个数组,如果需要,您甚至可以将数组传递给它:

var arrOfBarInterval = new BarInterval[] { val1, val2 };
var tracker = new BarListTracker(arrOfBarInterval);
但是,您也可以通过一个:

var tracker = new BarListTracker(barInterval);
如果你有能力做到这一点,我会推荐它


需要注意的一点是,
:此(…)
构造函数在您所在的构造函数之前被调用并执行。在构建逻辑时请记住这一点。

例如,它调用同一类的另一个构造函数

public class Point{
    public Point(int x, int y){
      this.x=x;
      this.y=y;
    }

    public Point():this(0,0){}
}
如果在代码中调用

var p= new Point();
您将使用定义的无参数构造函数,该构造函数将调用传递给他的参数为0,0的构造函数


如果您有多个接受大量参数的构造函数,并且希望为更简单的构造函数提供默认参数,那么这是非常有用的。您将有另一个以BarInterval数组为参数的构造函数。这基本上就是从另一个构造函数调用一个构造函数。另一个可能有用的链接是

这是构造函数重载的C#语法。该死的你这么快:)!谢谢你的帮助-标记为回答后timer@Rajeshwar,我很高兴能为您提供帮助,并感谢您的回答!祝你有美好的一天!