Im newer to C# and can't seem to find a direct answer to this:
So in a method I create an object reference to read an xml doc:
XElement xFinancialBill = xDoc.Root.Element("Financial_Transaction").Element("Bill");
It does some processing with the object and the method ends, but my question is: do I need to null out the above object reference (eg. xFinancialBill = null;
) before the method ends for garbage collection/freeing up memory to work correctly?
Generally you don't need to worry about object references once they have gone out of scope - the garbage collector will clear up.
However, if you are using an object whose class implements the IDisposable
interface then you need to make sure that it disposes of any unmanaged resources by either explicitly calling the IDisposable.Dispose()
method or by wrapping its usage in a using
block.
Look up IDisposable
in the MSDN for further explanation.