C#: should object variables be assigned to null?

前端 未结 8 966
别那么骄傲
别那么骄傲 2020-11-27 05:48

In C#, is it necessary to assign an object variable to null if you have finished using it, even when it will go out of scope anyway?

8条回答
  •  清酒与你
    2020-11-27 06:44

    Should you turn off your car before pushing it to the lake?
    No. It is a common mistake, but it doesn't make any difference. You aren't setting the object to null, just one reference to it - the object is still in memory, and must still be collected by the garbage collector.

提交回复
热议问题