I am calling function of the same class from constructor. But its giving me the error.
public class Connection
{
public Connection()
{
}
public Connection(string parameter) : this(GetConnection(parameter))
{
}
private static string GetConnection(string parameter)
{
return parameter;
}
}
But public Connection(string parameter) : this(GetConnection(parameter)) is giving me error.
The error is:
Constructor ‘Test.Connection.Connection(string)’ cannot call itself.
What is the error behind this. Is this type of calling possible??
Thanks!!!
`
You can call another constructor with this syntax (either this class constructor through
thiskeyword, or one of the available base class constructors throughbasekeyword). While you’re having a potential StackOverflowException here instead.You can simply do this:
Off topic: the following potential StackOverflowException is already not identifiable by compiler (i.e. it’s compiled without errors and warnings), but only at runtime:
Please read Using Constructors (C# Programming Guide) for more info:
Also see Calling base constructor in c#.