Do you need to dispose of objects and set them to null, or will the garbage collector clean them up when they go out of scope?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Objects will be cleaned up when they are no longer being used and when the garbage collector sees fit. Sometimes, you may need to set an object to
nullin order to make it go out of scope (such as a static field whose value you no longer need), but overall there is usually no need to set tonull.Regarding disposing objects, I agree with @Andre. If the object is
IDisposableit is a good idea to dispose it when you no longer need it, especially if the object uses unmanaged resources. Not disposing unmanaged resources will lead to memory leaks.You can use the
usingstatement to automatically dispose an object once your program leaves the scope of theusingstatement.Which is functionally equivalent to: