Search code examples
c#garbage-collectionnull

C#: should object variables be assigned to null?


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?


Solution

  • No, and that could in fact be dangerous and bug-prone (consider the possibility that someone might try to use it later on, not realizing it had been set to null). Only set something to null if there's a logical reason to set it to null.