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 ?
Using this with a Constructor From within a constructor, you can also use the this keyword to call another constructor in the same class.
A class constructor is a special member function of a class that is executed whenever we create new objects of that class. A constructor will have exact same name as the class and it does not have any return type at all, not even void.
Definition and Usage The this keyword refers to the current object in a method or constructor. The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter).
Constructors in C++ are the member functions that get invoked when an object of a class is created. There are mainly 3 types of constructors in C++, Default, Parameterized and Copy constructors.
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.
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 });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With