please consider this code :
1-...
2-{
3-...
4-SqlConnection cn=new SqlConnection();
5-...
6-}
if program reach to statement No.6 will cn dispose? if not what happend for cn variable?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
No.
It depends whether you use it afterwards. If you don’t use it and it falls out of scope which means that it is eligible for garbage collections. When this garbage collection happens is out of your control. But when it happens the destructor will be invoked which itself calls the
Disposemethod.This being said the correct approach is to always wrap
IDisposableresources inusingstatements. This ensures that the Dispose method will always be called, even if an exception is thrown: