开发者

Do I need to include the 'this' when using a property name in a closure?

I'm using a list of Actions to store an undo history for an object. Let's say I have a property of my object called myChildObject and it's being mutated by a method call, so I want to store the undo action where I would mutate it back to it's current value:

public class Class1
{
    public Class1()
    {
    }

    private readonly List<Action> m_undoActions = new List<Action>();

    private SomeObject myChildObject { get; set; }

    public void ChangeState()
    {
        m_undoActions.Add(() => myChildObject.UndoChangeState());
        myChildObject.ChangeState();
    }
}

Looking at the lambda expres开发者_JAVA技巧sion, is the reference to myChildObject (the object) passed or is the reference to 'this' passed. Do I need to use 'this' to preface it? Do I need to make a copy of the 'this' reference to a local variable first?

Thanks for helping me understand this closure stuff.


No, there's no more need to explicitly designate a member as an instance member within a lambda than there is outside of the lambda.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜