I was wondering as to what happens to an object (in C#), once its reference becomes reassigned. Example:
Car c = new Car(\"Red Car\");
c = new Car(\"Blue Car
The garbage collector will handle cleanup for the red car when it is not longer rooted (not reachable). You, the developer, don't generally have to worry about cleaning up memory in .Net.
There are three caveats that need to be mentioned:
using
statement is a good way to accomplish this.In case Car
holds some native resources you'll want to implement IDisposable
and dispose of it properly before reusing the variable.
I think you should implement the IDispose interface to clean up unmanaged resources
public class car : IDispose