Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can call <strong>another</strong> constructor with this syntax (either this class constructor through <code>this</code> keyword, or one of the available base class constructors through <code>base</code> keyword). While you're having a potential StackOverflowException here instead.</p> <p>You can simply do this:</p> <pre><code>public class Connection { public Connection() { } public Connection(string parameter) : this() { string connectionString = GetConnection(parameter); } } </code></pre> <hr> <p>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:</p> <pre><code>public class Connection { public Connection() : this(GetConnectionString()) { } public Connection(string parameter) : this() { } public static string GetConnectionString() { //... } } </code></pre> <p>Please read <a href="http://msdn.microsoft.com/en-us/library/ms173115%28v=vs.80%29.aspx" rel="nofollow noreferrer">Using Constructors (C# Programming Guide)</a> for more info:</p> <blockquote> <p>A constructor can invoke another constructor in the same object using the this keyword. Like base, this can be used with or without parameters, and any parameters in the constructor are available as parameters to this, or as part of an expression.</p> </blockquote> <p>Also see <a href="https://stackoverflow.com/questions/12051/calling-base-constructor-in-c-sharp">Calling base constructor in c#</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload