In a C# 2010 desktop application I have the following line of code that gets executed continually from calling a mthod:
Attachment att = helper.createAttachmentFromInput(strExcelLocation);
This method never complains about the 'att' object already existing. I never set the value to null.
This may not be causing an issue since when the method goes out of scope, the object is destroyed?
Can you tell me if my assumption is correct or not? Can you also explain your answer to me?
VulpesPosted Jan 21, 2013, 3:07 PM
'att' is a local variable i.e. a variable declared within a method. An Attachment object is created and a reference to that object is assigned to the variable.
Local variables only live (at most) until the method exits. When that happens, there will no longer be any references to the Attachment object which will therefore be destroyed in due course by the garbage collector and the memory it was using will be reclaimed.