Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h1>Question 1</h1> <p>The <code>??</code> is called the <a href="http://msdn.microsoft.com/en-us/library/ms173224.aspx" rel="nofollow noreferrer"><strong>null-coalescing operator</strong></a>, and is a very useful feature of C# 2.0 onwards.</p> <p>In your case,</p> <pre><code>FormsAuth = formsAuth ?? new FormsAuthenticationWrapper(); </code></pre> <p>simply means "assign <code>formsAuth</code> to <code>FormsAuth</code> unless it is null, in which case assign <code>new FormsAuthenticationWrapper()</code>". It's basically a way of preventing null references in your code. You can also think of it as a shortcut for the following conditional expression:</p> <pre><code>FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper(); </code></pre> <h1>Question 2</h1> <p>The use of <code>this(null, null)</code> is called <a href="http://www.yoda.arachsys.com/csharp/constructors.html" rel="nofollow noreferrer"><strong>constructor chaining</strong></a>. All this means is that that the constructor in the same class (hence <code>this</code>, as opposed to <code>base</code> for the parent class) that takes two parameters, should be called before the body of the constructor is executed.</p> <p>Overloading constructors is a common practice to make it easier for the developer to create new objects when they just want to use the default properties/settings.</p> <h1>Question 3</h1> <p>As others have mentioned, this really belongs as a separate question. Unlike the previous two, it's much more specific to the context/your code, rather than language features of C#.</p> <h1>Update</h1> <p>Ok, what I've done now is actually rewritten the two constructors here, since I think putting them in another (virtually equivalent) form might be a bit clearer, and is probably better design practice too. The null coalescing operator isn't necessary here.</p> <pre><code>public AuthenticationController() : this(new FormsAuthenticationWrapper(), Membership.Provider) { } public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider) { this.FormsAuth = formsAuth; this.Provider = provider; } </code></pre> <p>In this form, it should be obvious that the constructor that takes two parameters simply assigns the class variables to the values of the arguments. The parameterless constructor (often called the <em>default</em> constructor) simply creates a new object using the <em>default</em> <code>FormsAuth</code> and <code>Provider</code> objects, which are specified via <em>constructor chaining</em>.</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