In the following code, are the using blocks redundant or are they necessay to fully release resources?
using (var dialog = new AboutBox())
dialog.ShowDialog();
using (var form = new OptionForm())
form.Show();
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 example is not redundant. You should always dispose of an
IDisposablethe moment you are done with it and in the case of a modal form this exactly accomplishes the goal.The second example though will lead to errors. The
Showmethod returns immediately and the form continues to be displayed. However the generatedusingcode will immediatelyDisposethe form and cause it to go away. The form should only be disposed once it’s finished showing.