How to destroy an object in a condition? for example:
Car c = new Car()
if (something)
//destroy car
I have tried to set the object to null but it didn`t work...
How to destroy an object in a condition? for example:
Car c = new Car()
if (something)
//destroy car
I have tried to set the object to null but it didn`t work...
In addtion to setting your object as null , you should also remove it from other objects which refrences it as well if you class uses resources that needs to be freed up use IDisposable
public class Car: IDisposable
{
// free resources
public void Dispose()
{
}
}
set c = null;
As long as there are no other references to it, the garbage collector will destroy it the next time it runs.