Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Best practice for nested using statements?

I have a code block as follows and I'm using 3 nested using blocks.

I found that using try finally blocks I can avoid this but if there are more than two using statements, what is the best approach?

private FileStream fileStream = null;
private Document document = null;
private PdfWriter pdfWriter =  null;

using (fileStream = new FileStream("ABC.pdf", FileMode.Create))
{
    using (document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom))
    {
        using (pdfWriter = PdfWriter.GetInstance(document, fileStream))
        {
            document.AddAuthor(metaInformation["author"]);
            document.AddCreator(metaInformation["creator"]);
            document.AddKeywords("Report Generation using I Text");
            document.AddSubject("Document subject");
            document.AddTitle("The document title");
        }
    }
}
like image 833
Gayan Kalanamith Avatar asked Apr 21 '14 12:04

Gayan Kalanamith


3 Answers

You can remove the indention and curly brackets this way:

using (var fileStream = new FileStream("ABC.pdf", FileMode.Create))
using (var document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom))
using (var pdfWriter = PdfWriter.GetInstance(document, fileStream))
{
   // code
}
like image 70
Jan Sommer Avatar answered Nov 07 '22 09:11

Jan Sommer


A little less verbose way to avoid the indenting:

  using (var fileStream = new FileStream("ABC.pdf", FileMode.Create))
  using (var document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom))
  using (var pdfWriter = PdfWriter.GetInstance(document, fileStream))
  {
       document.AddAuthor(metaInformation["author"]);
       document.AddCreator(metaInformation["creator"]);
       document.AddKeywords("Report Generation using I Text");
       document.AddSubject("Document subject - Describing the steps creating a PDF document");
       document.AddTitle("The document title - PDF creation using iTextSharp");
   }

As Jon Skeet pointed out, there is no need for these variables to be instance variables, as they are disposed after the using blocks anyway.

You can use local variables as shown in the code above instead.

like image 29
thumbmunkeys Avatar answered Nov 07 '22 09:11

thumbmunkeys


Maybe something conventional; best approach for choosing between two in my opinion would be;

  • Using : If you are going to use an instance within a context and need to Dispose it after you are done with it
  • try/finally : If you are expecting any issue and have something to do with the exception, catching it before you Dispose the instance you are using.

And as other comments / answers state; you don't need instance level variables;

using (FileStream fileStream = new FileStream("ABC.pdf", FileMode.Create))
using (Document document = new Document(PageSize.A4, marginLeft, marginRight, marginTop, marginBottom))
using (PdfWriter pdfWriter = PdfWriter.GetInstance(document, fileStream))
{
    // # Implementation here seems like a good approach
}
like image 2
Saro Taşciyan Avatar answered Nov 07 '22 09:11

Saro Taşciyan