开发者

Why doesn't property set itself when class is instantiated

I have a property within my class that I would expect to set itself when a new instance of t开发者_StackOverflowhe class is created, but it doesn't, why?

public class RecurlyData
    {
        private readonly string _accountCode;

        //Default constructor
        public RecurlyData(int accountCode)
        {
            _accountCode = accountCode.ToString();
        }

        public RecurlyAccount Account { get { return GetAccount(); } }

        private RecurlyAccount GetAccount()
        {
            var account = RecurlyAccount.Get(_accountCode);
            account.BillingInfo = RecurlyBillingInfo.Get(account.AccountCode);

            return account;
        }
    }  

I am calling it like this:

private List<RecurlyData> _recurlyData;  
    _recurlyData.Add(new RecurlyData(1079));


I believe what you are expecting to happen is that GetAccount(); will be called when the object is constructed.

That is not how properties work.

A property's getter acts just like a method, so in fact your property

public RecurlyAccount Account { get { return GetAccount(); } }

Does the exact same thing as the GetAccount method.

Calling:

var myAccount = this.Account;

Is 100% identical to:

var myAccount = this.GetAccount();

If that method causes some visible side-effects (which I imagine it does, otherwise it wouldn't matter whether it gets called in the constructor or not) then it most likely should not be in a get property.

Every time that Account is accessed, the method will get called, so saying:

var data = new RecurlyData(1079);
var account = data.Account;
var account2 = data.Account;

The method GetAccount was called twice. The value isn't saved unless you write code to save it somewhere.

@pstrjds's answer should give you the behaviour you want, but as a slight alternative, if you don't like needing that private backing field, you can also write:

public class RecurlyData
{
    private readonly string _accountCode;

    public RecurlyData(int accountCode)
    {
        _accountCode = accountCode.ToString();
        Account = GetAccount(_accountCode);
    }

    public RecurlyAccount Account { get; private set; }

    private static RecurlyAccount GetAccount(string accountCode)
    {
        var account = RecurlyAccount.Get(accountCode);
        account.BillingInfo = RecurlyBillingInfo.Get(account.AccountCode);

        return account;
    }
}  

The result is almost exactly the same, with the exception that it's only private and not readonly so you could set it from somewhere other than the constructor. I do personally find it cleaner.


This should take care of creating the Account for you:

public class RecurlyData
{
    private readonly string _accountCode;
    private readonly RecurlyAccount _account;

    //Default constructor
    public RecurlyData(int accountCode)
    {
        _accountCode = accountCode.ToString();
        _account = GetAccount(_accountCode);
    }

    public RecurlyAccount Account { get { return _account; } }

    private static RecurlyAccount GetAccount(string accountCode)
    {
        var account = RecurlyAccount.Get(accountCode);
        account.BillingInfo = RecurlyBillingInfo.Get(account.AccountCode);

        return account;
    }
}  


readonly seems to be the one to be blamed. private is enough to protect the data from being modified.

EDIT: Nevermind, readonly has nothing to do.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜