Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling constructor from other constructor in same class

Tags:

c#

constructor

I have a class with 2 constructors:

public class Lens
{
    public Lens(string parameter1)
    {
        //blabla
    }

    public Lens(string parameter1, string parameter2)
    {
       // want to call constructor with 1 param here..
    }
}

I want to call the first constructor from the 2nd one. Is this possible in C#?

like image 432
Robbert Dam Avatar asked May 06 '09 14:05

Robbert Dam


People also ask

How can we call one constructor from another in same class?

The invocation of one constructor from another constructor within the same class or different class is known as constructor chaining in Java. If we have to call a constructor within the same class, we use 'this' keyword and if we want to call it from another class we use the 'super' keyword.

How do you call a constructor from another constructor?

Example 1: Java program to call one constructor from another Here, you have created two constructors inside the Main class. Inside the first constructor, we have used this keyword to call the second constructor. this(5, 2); Here, the second constructor is called from the first constructor by passing arguments 5 and 2.

Is it possible to call another constructor within a same class using a this keyword?

Java Constructor Chaining in the Same Class You can create multiple constructors in the same class, each with a different number of arguments that it accepts. To call one of the constructors in another constructor (of the same class), use the keyword this().


3 Answers

Append :this(required params) at the end of the constructor to do 'constructor chaining'

public Test( bool a, int b, string c )
    : this( a, b )
{
    this.m_C = c;
}
public Test( bool a, int b, float d )
    : this( a, b )
{
    this.m_D = d;
}
private Test( bool a, int b )
{
    this.m_A = a;
    this.m_B = b;
}

Source Courtesy of csharp411.com

like image 130
Gishu Avatar answered Oct 01 '22 18:10

Gishu


Yes, you'd use the following

public class Lens
{
    public Lens(string parameter1)
    {
       //blabla
    }

    public Lens(string parameter1, string parameter2) : this(parameter1)
    {

    }
}
like image 27
Matthew Dresser Avatar answered Oct 01 '22 17:10

Matthew Dresser


The order of constructor evaluation must also be taken into consideration when chaining constructors:

To borrow from Gishu's answer, a bit (to keep code somewhat similar):

public Test(bool a, int b, string c)
    : this(a, b)
{
    this.C = c;
}

private Test(bool a, int b)
{
    this.A = a;
    this.B = b;
}

If we change the evalution performed in the private constructor, slightly, we will see why constructor ordering is important:

private Test(bool a, int b)
{
    // ... remember that this is called by the public constructor
    // with `this(...`

    if (hasValue(this.C)) 
    {  
         // ...
    }

    this.A = a;
    this.B = b;
}

Above, I have added a bogus function call that determines whether property C has a value. At first glance, it might seem that C would have a value -- it is set in the calling constructor; however, it is important to remember that constructors are functions.

this(a, b) is called - and must "return" - before the public constructor's body is performed. Stated differently, the last constructor called is the first constructor evaluated. In this case, private is evaluated before public (just to use the visibility as the identifier).

like image 21
Thomas Avatar answered Oct 01 '22 19:10

Thomas