What best practices for cleaning up event handler references?

后端 未结 6 1647
一个人的身影
一个人的身影 2020-12-04 15:52

Often I find myself writing code like this:

        if (Session != null)
        {
            Session.KillAllProcesses();
            Session.AllUnitsReady          


        
6条回答
  •  难免孤独
    2020-12-04 16:12

    I found performing simple task like Globalizing the events which are most commonly used into its own class and inheriting their interfaces helps provide a developer a chance to use methods such as event properties for adding and removing events. Within your class whether encapsulation occurs or not clean up can begin by using something similar to the below example.

    E.g.

    #region Control Event Clean up
    private event NotifyCollectionChangedEventHandler CollectionChangedFiles
    {
        add { FC.CollectionChanged += value; }
        remove { FC.CollectionChanged -= value; }
    }
    #endregion Control Event Clean up
    

    This is an article that provides addition feedback to other uses for Property ADD REMOVE: http://msdn.microsoft.com/en-us/library/8843a9ch.aspx

提交回复
热议问题