Can I call an overloaded constructor from another constructor of the same class in C#?
Solution 1:
No, You can't do that, the only place you can call the constructor from another constructor in C# is immediately after ":" after the constructor. for example
class foo
{
public foo(){}
public foo(string s ) { }
public foo (string s1, string s2) : this(s1) {....}
}
Solution 2:
If you mean if you can do ctor chaining in C#, the answer is yes. The question has already been asked.
However it seems from the comments, it seems what you really intend to ask is
'Can I call an overloaded constructor from within another constructor with pre/post processing?'
Although C# doesn't have the syntax to do this, you could do this with a common initialization function (like you would do in C++ which doesn't support ctor chaining)
class A
{
//ctor chaining
public A() : this(0)
{
Console.WriteLine("default ctor");
}
public A(int i)
{
Init(i);
}
// what you want
public A(string s)
{
Console.WriteLine("string ctor overload" );
Console.WriteLine("pre-processing" );
Init(Int32.Parse(s));
Console.WriteLine("post-processing" );
}
private void Init(int i)
{
Console.WriteLine("int ctor {0}", i);
}
}