Can anyone please tell me how I can free objects in C#? For example, I have an object:
Object obj1 = new Object();
//Some code using obj1
/*
Here I would l
You don't have to. The runtime's garbage collector will come along and clean it up for you. That is why you are using C# and not unmanaged C++ in the first place :)
You do not. This is what a garbage collector does automatically - basically when the .NET runtime needs memory, it will go around and delete objects that are not longer in use.
What you have to do for this to work is to remove all linnks to the object.
In your case....
obj1=null;
at the end, then the object is no longer referenced and can be claimed from the garbage collector.
You can check http://en.wikipedia.org/wiki/Garbage_collection_(computer_science) for more details.
Note that if the object has references to unmanaged ressources (like open files etc.) it should implement the Disposable pattern (IDisposable interface) and you should explicitely release those references when you dont need the object anymore.
It's not recommended, but if you really need to, you can force garbage collection via:
GC.Collect();