Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

`this` and a class constructor

Tags:

c#

.net

oop

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 ?

like image 333
Rajeshwar Avatar asked Aug 02 '13 19:08

Rajeshwar


People also ask

Can you use this in a constructor?

Using this with a Constructor From within a constructor, you can also use the this keyword to call another constructor in the same class.

What is a class constructor?

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.

What does this () do in Java?

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).

What are the 3 types of constructor?

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.


2 Answers

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.

like image 133
Chris Sinclair Avatar answered Oct 14 '22 08:10

Chris Sinclair


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 });
like image 24
Sergey Kalinichenko Avatar answered Oct 14 '22 08:10

Sergey Kalinichenko