Suppose I have the following:
using(var ctx = DataContextFactory.Create(0))
{ ... Some code ... }
Why not just do the following and lose a couple of curly braces?:
var ctx = DataContextFactory.Create(0);
ctx.Dispose();
Thanks for the advice!
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.
The first is better. It ensures it is disposed even if an exception is thrown, and it correctly handles the case where
Create(0)returns null (i.e. it doesn’t attempt to callDispose()on a null instance).