Click here to Skip to main content
14,739,205 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Type 1:
objMortgageBAL = new MortgageBAL();
                int Result = objMortgageBAL.SaveLegalTeamComments(CauseNo, DefendantID);

Type 2:
int Result = new MortgageBAL().SaveLegalTeamComments(CauseNo, DefendantID);

i have used code in my project as mentioned both types above. When will "MortgageBAL()" object will dispose in Type2? Which one is good coding practice?

1 solution

In both cases, Dispose will probably happen at the same time - unless the definition of objMortgageBAL is at class level. If it is a local variable, then it will go out of scope at the end of the method, and is available for disposal at that point. That doesn't mean it will be Disposed, however!

Unless Dispose is explicitly (or implitly via a using block) called, reference items will not be disposed until the Garbage Collector runs out of space and decides that the object is no longer referenced.

If you want to control Disposal, then good practice would be:
using (objMortgageBAL = new MortgageBAL())
   int Result = objMortgageBAL.SaveLegalTeamComments(CauseNo, DefendantID);
In which case the MortgageBAL object will be disposed when it goes out of scope at the closing curly bracket.
HarisJayadev 1-Oct-12 6:49am
How to create IDisPosable class in layered architecture projects?

Mine 3 layered architecture. I have created DAL object in BAL and BAL in UI
OriginalGriff 1-Oct-12 6:58am
The only way to do it is to add the IDisposable interface to the class definition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900