How to transfer control to another constructor?

Here I have for example class:
public class test
{
 int _IntValue;
 string _StringValue;

 public test(int i)
{
_IntValue=i;
}

 public test(int i, string s)
{
 //here it is necessary to execute code from the first constructor
 _StringValue = s + _IntValue;
}
}

How do I make it so that when calling the second constructor first ran the code from the first, and only then from the second (or, conversely, are not)

Interested in the option without implementation through inheritance or through allocation code for the first constructor in a separate procedure.

Thank you.
October 3rd 19 at 02:19
3 answers
October 3rd 19 at 02:21
Solution

public test(int i, string s) : this(i)
{
 _StringValue = s + _IntValue;
}

*I promise to update the page before replying - Delmer.Jast2 commented on October 3rd 19 at 02:24
October 3rd 19 at 02:23
Solution

Guessed myself. Works similar to the transfer of control to the constructor of the parent, but instead base - this.


 public class test
{
 int _IntValue;
 string _StringValue;

 public test(int i)
{
 _IntValue = i;
}

 public test(int i, string s):this(i)
{
 _StringValue = s + _IntValue;
}
}

October 3rd 19 at 02:25

public class test
{
 int _IntValue;
 string _StringValue;

 public test(int i)
{
_IntValue=i;
}

 public test(int i, string s)
{
 //here it is necessary to execute code from the first constructor
this.test(i);

 _StringValue = s + _IntValue;
}
}

Something does not work.. Error 3 the "KOR.test" does not contain a definition for "test" and was not found an extension method "test" host type "KOR.test" as the first argument (possibly missing a using Directive or an Assembly reference) - Delmer.Jast2 commented on October 3rd 19 at 02:28

Find more questions by tags C#