Where did variable = null as “object destroying” come from?

后端 未结 14 1844
无人共我
无人共我 2021-02-18 15:51

Working on a number of legacy systems written in various versions of .NET, across many different companies, I keep finding examples of the following pattern:

pub         


        
14条回答
  •  暗喜
    暗喜 (楼主)
    2021-02-18 16:23

    Consider a slight modification:

    public void FooBar() 
    { 
        object foo = null; 
        object bar = null; 
    
        try 
        { 
           foo = new object(); 
           bar = new object(); 
    
           // Code which throws exception. 
        } 
        finally 
        { 
           // Destroying objects 
           foo = null; 
           bar = null; 
        } 
        vavoom(foo,bar);
    } 
    

    The author(s) may have wanted to ensure that the great Vavoom (*) did not get pointers to malformed objects if an exception was previously thrown and caught. Paranoia, resulting in defensive coding, is not necessarily a bad thing in this business.

    (*) If you know who he is, you know.

提交回复
热议问题