I’m learning C#. From what I know, you have to set things up correctly to have the garbage collector actually delete everything as it should be. I’m looking for wisdom learned over the years from you, the intelligent.
I’m coming from a C++ background and am VERY used to code-smells and development patterns. I want to learn what code-smells are like in C#. Give me advice!
What are the best ways to get things deleted?
How can you figure out when you have ‘memory leaks’?
Edit: I am trying to develop a punch-list of ‘stuff to always do for memory management’
Thanks, so much.
C#, the .NET Framework uses Managed Memory and everything (but allocated unmanaged resources) is garbage collected.
It is safe to assume that managed types are always garbage collected. That includes
arrays,classesandstructures. Feel free to doint[] stuff = new int[32];and forget about it.If you open a file, database connection, or any other unmanaged resource in a class, implement the IDisposable interface and in your Dispose method de-allocate the unmanaged resource.
Any class which implements IDisposable should be explicitly closed, or used in a (I think cool) Using block like;
Here .NET will dispose reader when out of the { } scope.