Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 833537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:32:03+00:00 2026-05-15T04:32:03+00:00

Is it ok to instance a MemoryStream at the top of my method, do

  • 0

Is it ok to instance a MemoryStream at the top of my method, do a bunch of stuff to it, and then use it?

For instance:

public static byte[] TestCode()
{
  MemoryStream m = new MemoryStream();
  ...
  ...
  whole bunch of stuff in between
  ...
  ...

  //finally

  using(m)
  {
     return m.ToArray();
  }
}

Updated code

public static byte[] GetSamplePDF()
{
    using (MemoryStream m = new MemoryStream())
    {
        Document document = new Document();

        PdfWriter.GetInstance(document, m);

        document.Open();

        PopulateTheDocument(document);

        document.Close();

        return m.ToArray();
    }
}

private static void PopulateTheDocument(Document document)
{
    Table aTable = new Table(2, 2);
    aTable.AddCell("0.0");
    aTable.AddCell("0.1");
    aTable.AddCell("1.0");
    aTable.AddCell("1.1");

    document.Add(aTable);

    for (int i = 0; i < 20; i++)
    {
        document.Add(new Phrase("Hello World, Hello Sun, Hello Moon, Hello Stars, Hello Sea, Hello Land, Hello People. "));
    }

}

My point was to try to reuse building the byte code. In other words, build up any kind of document and then send it to TestCode() method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T04:32:04+00:00Added an answer on May 15, 2026 at 4:32 am

    Technically, this is possible, but it’s pointless. If you really want to avoid using the “using” statement around that code, just call Dispose() directly.

    You should put the entire work that’s using the MemoryStream into the using statement. This guarantees that the MemoryStream’s Dispose method will be called, even if you receive an exception during your “whole bunch of stuff in between” code. The way you have it now, exceptions will prevent your MemoryStream from having Dispose() called on it.

    The proper way to handle this would be:

    public static byte[] TestCode()
    {
      MemoryStream m = new MemoryStream();
      using(m)
      {
          // ...
          // ...
          // whole bunch of stuff in between
          // ...
          // ...
    
         return m.ToArray();
      }
    }
    

    Or, in the more common form:

    public static byte[] TestCode()
    {
      using(MemoryStream m = new MemoryStream())
      {
          // ...
          // ...
          // whole bunch of stuff in between
          // ...
          // ...
    
         return m.ToArray();
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For instance, if I have an class: public class StuffHolder { List<Stuff> myList; public
For instance, consider the DFT or DCT. Precisely, what would be the differences between
For instance you chose X,Y and Z colors, and then websites shows you a
I have a method here that is supposed to produce a System.Drawing.Image instance. Consider
The following code will always throw UnuthorizedAccessException (MemoryStream's internal buffer cannot be accessed.) byte[]
For a .Net MemoryStream object instance, do I need to close it explicitly after
I got the following code: public class Alarm:IDisposable { MemoryStream _tmp; readonly XmlDocument _doc;
Assume I have copied a byte buffer into a memory stream using this memoryStream.Read(data,
If I write the following code: public void Execute() { var stream = new
I have an instance of MemoryStream that is closed (don't ask, can't change that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.