Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 483
Craig Johnston Avatar asked Oct 11 '10 06:10

Craig Johnston


1 Answers

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.

like image 53
Daniel DiPaolo Avatar answered Sep 26 '22 20:09

Daniel DiPaolo