---
title: "[Tip]:MemoryStream.GetBuffer() vs. MemoryStream.ToArray()"
url: "https://osherove.com/blog/2003/10/7/tipmemorystreamgetbuffer-vs-memorystreamtoarray.md"
canonical: "https://osherove.com/blog/2003/10/7/tipmemorystreamgetbuffer-vs-memorystreamtoarray.html"
published: "2003-10-07T01:07:00"
updated: "2003-10-07T01:07:00"
tags: [".Net Original"]
author: "Roy Osherove"
description: "One of my readers posted a comment on my Site-To-RSS article that just solved me a bunch of trouble! In the article, I was using a memory stream to write the XML into memory. I was using the MemoryStream.GetBuffer()…"
usage: "Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page)."
license: "https://creativecommons.org/licenses/by/4.0/"
---

# \[Tip\]:MemoryStream.GetBuffer() vs. MemoryStream.ToArray()

By Roy Osherove October 7, 2003 In [.Net Original](https://osherove.com/blog/category/net-original)

**Historical article** · Originally published October 7, 2003; technical details may be outdated.

One of my readers posted a comment on my [Site-To-RSS article](http://weblogs.asp.net/rosherove/posts/29509.aspx) that just solved me a bunch of trouble!

In the article, I was using a memory stream to write the XML into memory. I was using the _MemoryStream.GetBuffer()_ method to retrieve the text inside the stream. This caused some ugly code because to get the buffer correctly, I had to know the length of the actual string in the stream, meaning the stream had to be open for me to know that. Even so, it didn't work very well and sometimes retrieved corrupted data.

So, TheCShark (huh?) solved all my problems in one fell swoop by posting one simple line:

_Tip: use **ToArray** instead of **GetBuffer**, **ToArray** works when the MemoryStream is closed._

**Amazing**. All my problems are now solved:

- No longer need to keep stream open
- Calling code is much simpler:

- Dim

    retval As String = Encoding.UTF8.GetString(ms.ToArray())

- It works without a hitch!

So, we learned that we should **always use ToArray() to get the value**. Always. Phew. How silly of me not to notice that!

Thanks, CShark!

## More in .Net Original

[All posts in .Net Original](https://osherove.com/blog/category/net-original)

- Oct 15, 2003

    ### [\[Article\] on MSDNAA: Creating a Generic Site-To-Rss Tool](https://osherove.com/blog/2003/10/15/article-on-msdnaa-creating-a-generic-site-to-rss-tool.html)

- Oct 8, 2003

    ### [Introducing SiteFeeder](https://osherove.com/blog/2003/10/8/introducing-sitefeeder.html)

- Oct 6, 2003

    ### [When running 'aspnet\_regiis -c' still doesn't fix it...](https://osherove.com/blog/2003/10/6/when-running-aspnet_regiis-c-still-doesnt-fix-it.html)

- Oct 5, 2003

    ### [Fun with NUnit - clarification](https://osherove.com/blog/2003/10/5/fun-with-nunit-clarification.html)


---

## About the author

**Roy Osherove**: Senior Solutions Architect at AWS working on AI for production and AI-driven development; author of The Art of Unit Testing and Elastic Leadership, with about 20 years of consulting and training on unit testing, TDD, team leadership and continuous delivery.

### Sites

- [RobotPaper](https://robotpaper.ai/): This blog: AI patterns and anti-patterns, AI and coding agents, and engineering leadership.
- [osherove.com](https://osherove.com): Roy's personal site. Author of The Art of Unit Testing and Elastic Leadership; consulting and training on unit testing, TDD, team leadership and continuous delivery, plus about 20 years of blog posts.
- [The Art of Unit Testing](https://www.artofunittesting.com): The Art of Unit Testing, 3rd Edition (Manning): readable, maintainable, trustworthy unit tests, plus TDD workshops in C#, JavaScript, Java and Kotlin.
- [5whys.com](https://www.5whys.com): Roy's writing on software team leadership: self-organizing teams, surviving chaos, learning mode and the Elastic Leadership model.
- [Pipeline Driven](https://pipelinedriven.org): Continuous delivery and DevOps practices: build patterns, pipeline-friendly testing, DevOps metrics and enterprise DevOps.
- [Elastic Leadership](https://www.elasticleadership.com): Elastic Leadership, Roy's framework for software team leaders (survival, learning and self-organizing modes).

### Social

- X: https://x.com/royosherove
- LinkedIn: https://www.linkedin.com/in/osherove/

### Contact

- Contact form: https://forms.gle/jQSx5KoXvrVA1JCK7

### License

Licensed under CC BY 4.0 — reuse, quote and adapt with attribution (Roy Osherove, link to the original page). License: https://creativecommons.org/licenses/by/4.0/
