开发者

MemoryStream instance timing help

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.GetInstan开发者_JS百科ce(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.


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();
  }
}


When seeing questions like these, I often wonder if we wouldn't have been better off using the Java model. There's an extraordinary amount of agony that .NET programmers suffer over that doggone IDisposable. After thousands of questions on SO (et al), it still remains poorly understood.

It is a memory stream. There's nothing that needs to be disposed when you use memory, the garbage collector already takes care of it. It is not some kind of special memory just because the class has a Dispose() method, there's only one kind. The other kind is wrapped by UnmanagedMemoryStream. The fact that MemoryStream inherits a do-nothing Dispose() method from Stream is a sad OOP liability.

It is up to you to decide to slovenly call a do-nothing method because it is there. Or you could take charge of your code and refuse to call methods that you know don't do anything useful now, nor will ever do anything useful for the rest of your career. Clearly I'm in the second camp, our life-expectancy must be better. I hope anyway. Then again, this post might have knocked a day off.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜