Need Help understanding this code

感情迁移 提交于 2019-12-06 14:18:46

Question 1

The ?? is called the null-coalescing operator, and is a very useful feature of C# 2.0 onwards.

In your case,

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

simply means "assign formsAuth to FormsAuth unless it is null, in which case assign new FormsAuthenticationWrapper()". 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:

FormsAuth = formsAuth != null ? formsAuth : new FormsAuthenticationWrapper();

Question 2

The use of this(null, null) is called constructor chaining. All this means is that that the constructor in the same class (hence this, as opposed to base for the parent class) that takes two parameters, should be called before the body of the constructor is executed.

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.

Question 3

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#.

Update

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.

public AuthenticationController()
    : this(new FormsAuthenticationWrapper(), Membership.Provider)
{
}

public AuthenticationController(IFormsAuthentication formsAuth,
    MembershipProvider provider)
{
    this.FormsAuth = formsAuth;
    this.Provider = provider;
}

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 default constructor) simply creates a new object using the default FormsAuth and Provider objects, which are specified via constructor chaining.

Question 1: ?? is the null coalescing operator. The ?? operator checks whether the value provided on the left side of the expression is null, and if so it returns an alternate value indicated by the right side of the expression.

In your situation, it checks if formsAuth is null, and returns a new FormsAuthenticationWrapper() if it is null.

The ?? operator is saying "use this, unless it's null, it which case use this other thing".

So, this line of code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

Is the same as:

if ( formsAuth != null ) FormsAuth = formsAuth
else FormsAuth = new FormsAuthenticationWrapper();

In answer to Q2

It is overloading the constructor.

If means that calling

Foo() 

is the same as calling

Foo(null, null)

Question 1: The ?? operator simply says "take whatever is on my left if it's not null - if it is, take whatever is on my right". So your code:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();

is equivalent to

if (formsAuth != null) {
    FormsAuth = formsAuth;
} else {
    FormsAuth 0 new FormsAuthenticationWrapper();
}

Question 2: The :this(null, null) syntax is shorthand for "constructor inheritance" (my naming...). Your code

public AuthenticationController(): this(null, null)
    {
    }
public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider  provider)
    {
        FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
        Provider = provider ?? Membership.Provider;
    }

is equivalent to

public AuthenticationController()
    {
        FormsAuth = new FormsAuthenticationWrapper();
        Provider = Membership.Provider;
    }
public AuthenticationController(IFormsAuthentication formsAuth, MembershipProvider provider)
    {
        FormsAuth = formsAuth;
        Provider = provider;
    }

Question 2

public AuthenticationController(): this(null, null)
{
}

The no parameter constructor for AuthenticationController will call the constructor that takes a IFormsAuthentication and a MembershipProvider, passing two null values (this is done before any code in the no-param constructor code block is executed). Since the two argument constructor uses the null-coalescing (??) operator to assign the variables and the passed arguments are null, a new MembershipProvider is used along with Membership.Provider object.

Had this constructor not been explicitly defined, the default no-param constructor would have been used. This could lead to unexpected behaviour if a new AuthenticationController was created (without passing any arguments to the constructor), since the member variables would not have been initialised.

Question 1:

FormsAuth = formsAuth ?? new FormsAuthenticationWrapper();
Provider = provider ?? Membership.Provider;

is equals to:

FormsAuth = (formsAuth == null ? new FormsAuthenticationWrapper() : formsAuth);
Provider = (provider == null ? Membership.Provider : provider);

Question 2:

It's just passing null to both formsAuth and provider constructor arguments. It's not good practice IMHO. Another constructor with no arguments would be a better fit.

EDIT: This makes no sense. Sorry, I was in a hurry and didn't really realize it was a constructor calling another constructor.

I don't have time to answer question 3 right now, I'll get to that later...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!