简体   繁体   中英

What does this C# code do?

    public class AccountMembershipService : IMembershipService
    {
        private readonly MembershipProvider _provider;

        public AccountMembershipService()
            : this(null)
        {
        }

I took this bit of code from the AccountModels.cs class automatically created with the MVC3 project.

Can you explain what the 'this(null)' bit is doing?

It will call the single-argument constructor for AccountMembershipService , passing a null as the argument, before processing the body of the constructor you list.

From MSDN :

A constructor can invoke another constructor in the same object by 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.

More detail in section 17.10.1 (Constructor initializers) of the C# spec .

There is most likely another constructor in your class that looks like this:

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider;
}

Your code calls this constructor first and passes null as the argument for provider , then executes the original constructor.

它使用null参数调用其他构造函数。

The above answers are answering the question asked, but to go a bit further:

This is one technique we use for inversion of control and making unit testing possible.

Here are both constructors

public AccountMembershipService()
    : this(null)
{
}

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider ?? Membership.Provider;
}

The first constructor with :this(null) calls your 2nd constructor, passing null to the parameter provider.

One reason for doing this is to avoid duplication of logic. Suppose you did:

public AccountMembershipService()
{
    _provider = Membership.Provider;
}

public AccountMembershipService(MembershipProvider provider)
{
    _provider = provider;
}

While perfectly reasonable, if you change the name of _provider, or perhaps add some other initialization code, you'd have to modify it in 2 places. By calling :this(null), now all your work just happens in one place.

The reason we have 2 constructors is, by calling the default constructor, the static instance Membership.Provider gets used. Unfortunately it would be very difficult to unit test, because now we have dependencies on the membership providers, on the database, on having valid data, etc.

Instead, by creating the 2nd constructor, we can now pass in a mock MembershipProvider. This is an entirely different topic though, so if you're interested in how that works, feel free to ask another question.

It calls another constructor in your class and passes null as a parameter.

You can also write : base(...) to explicitly call a constructor in your base class.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM