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

后端 未结 14 1881
无人共我
无人共我 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:33

    I suspect that this pattern comes from translating C++ code to C# without pausing to understand the differences between C# finalization and C++ finalization. In C++ I often null things out in the destructor, either for debugging purposes (so that you can see in the debugger that the reference is no longer valid) or, rarely, because I want a smart object to be released. (If that's the meaning I'd rather call Release on it and make the meaning of the code crystal-clear to the maintainers.) As you note, this is pretty much senseless in C#.

    You see this pattern in VB/VBScript all the time too, for different reasons. I mused a bit about what might cause that here:

    http://blogs.msdn.com/b/ericlippert/archive/2004/04/28/122259.aspx

提交回复
热议问题