6

I currently have a class and I am a bit confused with its constructor .

 public class BarListTracker : GotTickIndicator
 {
    public BarListTracker(BarInterval interval) : this(new BarInterval[] { interval }) { }
 }

what does the statement this(new BarInterval[] { interval }) imply ?

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
Rajeshwar
  • 11,179
  • 26
  • 86
  • 158
  • 2
    This is C# syntax for constructor overloading. http://stackoverflow.com/a/5555741/1445661 – mao47 Aug 02 '13 at 19:13

6 Answers6

14

This is constructor chaining. Essentially, you're calling a different constructor before executing the contents of that constructor.

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

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

new Foo(); //outputs "Hello World!"

So somewhere in your BarListTracker there should be another constructor that takes either a BarInterval[] array or an IEnumerable<BarInterval> like this:

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
    }
}

It will execute the body BarListTracker(BarInterval[]), then execute the body of BarListTracker(BarInterval)

This is generally used to reduce code duplication. If you had some initialization code for your BarListTracker, it makes more sense to write it in one place and share that logic with your constructors rather than rewriting it for each one.

In addition, it lets you pass in or modify the input parameters with basic expressions. So in this case, in-line with calling the BarListTracker(BarInterval[]) constructor, it's wrapping the single BarInterval interval object as an array to match the signature. Likely this is just a convenience overload to provide a simpler API for programmers who may often have only a single BarInterval to construct the tracker with.

Chris Sinclair
  • 22,858
  • 3
  • 52
  • 93
4

This means a call of another constructor of the BarListTracker that takes an array of BarInterval objects, passing in an array containing the object passed into this constructor. It makes the call

var tracker = new BarListTracker(interval);

equivalent to this:

var tracker = new BarListTracker(new BarInterval[] { interval });
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
2

Call another constructor in this class, something like this:

public BarListTracker(BarInterval[] intervals)
zs2020
  • 53,766
  • 29
  • 154
  • 219
2

That is indicating that they are basing, or calling, another constructor in the class when this one gets called and passing the value as an array of BarInterval. In this case it's not a base class because otherwise it would say : base(...), it's another constructor defined in this very same class.

This is very common because you want to access a class a number of different ways, and in this case, it appears they wanted to be able to send just a single object at times without setting up an array in the code.

However, one thing they could have done is just changed the other constructor, the one being called with : this to be this:

public BarListTracker(params BarInterval[] interval)

and they wouldn't have even needed the second constructor. It's a cleaner solution and yields the same results everywhere. The other constructor still gets an array, you can even pass an array to it if you wanted:

var arrOfBarInterval = new BarInterval[] { val1, val2 };
var tracker = new BarListTracker(arrOfBarInterval);

But, you can also just pass one:

var tracker = new BarListTracker(barInterval);

If you have the ability to do that I would recommend it.

One caveat to note, the : this(...) constructor gets called and executed before the constructor you're in. Keep that in mind when building logic.

Mike Perrenoud
  • 66,820
  • 29
  • 157
  • 232
1

It calls another constructor of the same class for example

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

    public Point():this(0,0){}
}

if in your code you call

var p= new Point();

you will use the parameterless constructor defined which will call the constructor with parameter passing to him 0,0

this is pretty usefull if you have more than one constructor which accept a lot of parameter and want to provide simpler constructor with default parameter

Fabio Marcolini
  • 2,315
  • 2
  • 24
  • 30
0

you will have another constructor that takes array of BarInterval as parameter . This is basically nothing but calling a constructor from another constructor . Another link that might be helpful is Call one constructor from another

Community
  • 1
  • 1
Kinaan Khan Sherwani
  • 1,504
  • 16
  • 28