`this` and class constructor

I currently have a class, and I'm a little confused by its constructor.

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

What does this(new BarInterval[] { interval }) expression mean?

+6
source share
6 answers

This indicates that they base or call another constructor in the class when this call is called and passes the value in the form of an array of BarInterval . In this case, it is not a base class, because otherwise it would say : base(...) , this is another constructor defined in the same class.

This is very common because you want to access the class in several different ways, and in this case it seems that they want to be able to send only one object at a time, without tuning the array in the code.

However, one thing they could do is simply change another constructor, one that is called with : this , as follows:

 public BarListTracker(params BarInterval[] interval) 

and they wouldn’t even need a second constructor. This is a cleaner solution and gives the same results worldwide. Another constructor still gets an array, you can even pass an array to it if you want:

 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 opportunity to do this, I would recommend it.

It should be noted that the constructor : this(...) is called and executed before the constructor you are in. Keep this in mind when building logic.

+2
source

This is a chain of constructors . Essentially, you call another 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 should be another constructor that accepts either the BarInterval[] array or IEnumerable<BarInterval> as follows:

 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 of BarListTracker(BarInterval[]) , then it will execute the body of BarListTracker(BarInterval)

This is usually used to reduce code duplication. If you have an initialization code for your BarListTracker , it makes sense to write it in one place and share this logic with your designers, rather than rewrite it for each of them.

In addition, it allows you to transfer or change input parameters using basic expressions. Thus, in this case, in a line with the call to the BarListTracker(BarInterval[]) constructor BarListTracker(BarInterval[]) it wraps a single BarInterval interval object in the form of an array so that it matches the signature. This is probably just a convenient overload to provide a simpler API for programmers who often can only have one BarInterval to create a tracker with.

+14
source

This means calling another BarListTracker constructor, which takes an array of BarInterval objects, passing it to an array containing the object passed to this constructor. He is calling

 var tracker = new BarListTracker(interval); 

equivalent to this:

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

Calling another constructor in this class, something like this:

 public BarListTracker(BarInterval[] intervals) 
+2
source

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 the code you call

 var p= new Point(); 

you will use a constructor without parameters, which will call the constructor with the parameter 0,0

This is very useful if you have several constructors that accept many parameters and want to provide a simpler constructor with a default parameter.

+1
source

you will have another constructor that takes an array of BarInterval as a parameter. This is basically nothing but calling a constructor from another constructor. Another link that may be useful, Calling one constructor from another

0
source

Source: https://habr.com/ru/post/950929/


All Articles