{"id":84718,"date":"2023-03-20T07:00:57","date_gmt":"2023-03-20T06:00:57","guid":{"rendered":"https:\/\/code-maze.com\/?p=84718"},"modified":"2023-03-20T08:09:49","modified_gmt":"2023-03-20T07:09:49","slug":"csharp-memorystream","status":"publish","type":"post","link":"https:\/\/code-maze.com\/csharp-memorystream\/","title":{"rendered":"How to Use MemoryStream in C#"},"content":{"rendered":"<p>MemoryStream in C# is a class that provides a stream implementation for in-memory data and offers <strong>several benefits over traditional file-based streams<\/strong>. This article will teach why, when, and how to use MemoryStream class in C#.<\/p>\n<div style=\"padding: 20px; border-left: 5px #dc2323 solid; display: block; margin-bottom: 20px; box-shadow: 1px 1px 5px 0px lightgrey;\">To download the source code for this article, you can visit our <a href=\"https:\/\/github.com\/CodeMazeBlog\/CodeMazeGuides\/tree\/main\/csharp-intermediate-topics\/MemoryStreamInCsharp\" target=\"_blank\" rel=\"nofollow noopener\">GitHub repository<\/a>.<\/div>\n<p>Let&#8217;s dive in.<\/p>\n<h2>What is MemoryStream?<\/h2>\n<p><code>MemoryStream<\/code> is a class in .NET that stores data in the system\u2019s memory. It <strong>provides a stream-based mechanism and is used to handle data efficiently<\/strong>.\u00a0<\/p>\n<p><code>MemoryStream<\/code>\u00a0implements the <code>Stream<\/code> interface. Therefore, it implements methods and properties that allow us to <strong>read, write, and seek data in the system\u2019s memory<\/strong>. It also provides a buffer that is used to store the data. Additionally, we can access the data stored in the buffer both sequentially or randomly.<\/p>\n<h2>MemoryStream Advantages<\/h2>\n<p><code>MemoryStream<\/code> has several advantages over other methods of storing data. For one, it is <strong>highly efficient, user-friendly, secure, flexible, and reliable<\/strong>. Additionally, we can use it in many applications, such as storing data in memory, processing data, storing large amounts of data, sending data over the network, and accessing data in a database.<\/p>\n<p>One of the main advantages of using <code>MemoryStream<\/code> is its high performance. Since it works directly with memory, it can also read and write data more quickly than other stream mechanisms. If you would like to learn more about Stream-related topics, please check out our article on <a href=\"https:\/\/code-maze.com\/csharp-basics-streamwriter-streamreader\/\" target=\"_blank\" rel=\"noopener\">StreamWriter and StreamReader<\/a><\/p>\n<p>The second advantage of the <code>MemoryStream<\/code> class is the simplicity of its implementation. Through our examples, we will see how simple it is to create and use <code>MemoryStream<\/code> in our projects.\u00a0<\/p>\n<p>Another advantage is that <code>MemoryStream<\/code> <strong>supports random access<\/strong>. That means we can access data in any order rather than just reading or writing it sequentially.<\/p>\n<p>Finally, since a <code>MemoryStream<\/code> works with in-memory data, we can use it to manipulate data without additional I\/O operations.<\/p>\n<h2>How to Create MemoryStream in C#?<\/h2>\n<p>Creating and using <code>MemoryStream<\/code> is relatively easy. <code>MemoryStream<\/code> is a class that implements the <code>Stream<\/code> interface, providing methods and properties that allow us to read, write, and seek data in the system\u2019s memory.<\/p>\n<p><code>MemoryStream<\/code> has several overloaded constructors:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public MemoryStream();\r\npublic MemoryStream(byte[] buffer);\r\npublic MemoryStream(int capacity);\r\npublic MemoryStream(byte[] buffer, bool writable);\r\npublic MemoryStream(byte[] buffer, int index, int count);\r\npublic MemoryStream(byte[] buffer, int index, int count, bool writable);\r\npublic MemoryStream(byte[] buffer, int index, int count, bool writable, bool publiclyVisible);<\/pre>\n<p>The possible parameters are:<\/p>\n<ul>\n<li>buffer &#8211; an array of unsigned bytes used to create the stream<\/li>\n<li>capacity &#8211; the initial size of the internal array in bytes<\/li>\n<li>index &#8211; start position in the buffer at which the stream begins<\/li>\n<li>count &#8211; the length of the stream<\/li>\n<li>writable &#8211; determines if the stream supports writing<\/li>\n<li>publiclyVisible &#8211; if true, <code>GetBuffer()<\/code>, a method that returns the unsigned <a href=\"https:\/\/code-maze.com\/csharp-bitarray\/\" target=\"_blank\" rel=\"noopener\">byte array<\/a> from which the stream was created is enabled\u00a0<\/li>\n<\/ul>\n<p>It might be useful to note that <code>MemoryStream<\/code> implements an <code>IDisposable<\/code> interface. However, <strong>it does not hold any resources that should be disposed of<\/strong>. In practice, it is not necessary to call the <code>Dispose()<\/code> method on it or use the class inside a <code>using<\/code> statement. For more detail regarding this, please check the\u00a0<a href=\"https:\/\/learn.microsoft.com\/en-us\/dotnet\/api\/system.io.memorystream?view=net-7.0\" target=\"_blank\" rel=\"nofollow noopener\">official documentation<\/a>.<\/p>\n<p>Now, let&#8217;s see some examples of this.<\/p>\n<p>First, let&#8217;s create a class <code>Constructors<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static class Constructors\r\n{\r\n    public static MemoryStream SimpleConstructor() =&gt; \r\n        new MemoryStream();\r\n\r\n    public static MemoryStream ByteArrayConstructor(byte[] bytes) =&gt; \r\n        new MemoryStream(bytes);\r\n\r\n    public static MemoryStream FullConstructor(byte[] bytes, int count) =&gt; \r\n        new MemoryStream(bytes, 0, count, true, true);\r\n}<\/pre>\n<p>Here we define three methods we will use to create <code>MemoryStream<\/code> objects.<\/p>\n<p>Then, to make it easier for us to view <code>MemoryStream<\/code> properties, let&#8217;s define a method <code>ShowMemoryStreamProperties()<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static string ShowMemoryStreamProperties(MemoryStream memoryStream, string comment = \"\")\r\n{\r\n    var sb = new StringBuilder();\r\n    if (!string.IsNullOrEmpty(comment))\r\n        sb.AppendLine($\"{comment}\\n--------------------------\");\r\n\r\n    sb.AppendLine($\"{\"Length:\",-20}{memoryStream.Length}\");\r\n    sb.AppendLine($\"{\"Capacity:\",-20}{memoryStream.Capacity}\");\r\n    sb.AppendLine($\"{\"CanRead:\",-20}{memoryStream.CanRead}\");\r\n    sb.AppendLine($\"{\"CanSeek:\",-20}{memoryStream.CanSeek}\");\r\n    sb.AppendLine($\"{\"CanWrite:\",-20}{memoryStream.CanWrite}\");\r\n    sb.AppendLine($\"{\"CanTimeout:\",-20}{memoryStream.CanTimeout}\");\r\n    sb.AppendLine($\"{\"publiclyVisible:\",-20}{memoryStream.TryGetBuffer(out _)}\");\r\n\r\n    return sb.ToString();\r\n}<\/pre>\n<p>This method returns a string that shows the values of <code>MemoryStream<\/code> properties.<\/p>\n<p>Let&#8217;s start with the basic constructor and analyze <code>MemoryStream<\/code> properties:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var memoryStream = Constructors.SimpleConstructor();\r\nvar displayProperties = Methods.ShowMemoryStreamProperties(memoryStream,\r\n    \"Simple Constructor\");<\/pre>\n<p>The result of the <code>ShowMemoryStreamProperties()<\/code> method is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Simple Constructor\r\n--------------------------\r\nLength:             0\r\nCapacity:           0\r\nCanRead:            True\r\nCanSeek:            True\r\nCanWrite:           True\r\nCanTimeout:         False\r\npubliclyVisible:    True\r\n<\/pre>\n<p>Here we can see that by default, our <code>MemoryStream<\/code> has the <code>Length<\/code> and <code>Capacity<\/code> of zero. Furthermore, we can see the parameters <code>CanRead<\/code>, <code>CanWrite<\/code>, and <code>CanSeek<\/code> set to <code>true<\/code>. Finally, we can see the parameter <code>CanTimeout<\/code> is set to <code>false<\/code>, and the parameter <code>publiclyVisible<\/code> is set to <code>true<\/code>, which consequently means that the <code>GetBuffer()<\/code> method is enabled.<\/p>\n<p>Let&#8217;s see how to create a <code>MemoryStream<\/code> from byte array:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var phrase1 = \"How to Use MemoryStream in C#\";\r\nvar phrase1Bytes = Encoding.UTF8.GetBytes(phrase1);\r\n\r\nmemoryStream = Constructors.ByteArrayConstructor(phrase1Bytes);\r\ndisplayProperties = Methods.ShowMemoryStreamProperties(memoryStream,\r\n    \"Constructed From byte array\");<\/pre>\n<p>Our new <code>MemoryStream<\/code> properties are:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Constructed From byte array\r\n--------------------------\r\nLength:             29\r\nCapacity:           29\r\nCanRead:            True\r\nCanSeek:            True\r\nCanWrite:           True\r\nCanTimeout:         False\r\npubliclyVisible:    False<\/pre>\n<p>In this case, <code>publiclyVisible<\/code> parameter is set to <code>false<\/code>. Consequently, this means its <code>GetBuffer()<\/code> method is disabled.\u00a0<\/p>\n<p>Finally, let&#8217;s see the last overloaded <code>MemoryStream<\/code> constructor:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">memoryStream = Constructors.FullConstructor(phrase1Bytes, phrase1Bytes.Length - 10);\r\ndisplayProperties = Methods.ShowMemoryStreamProperties(memoryStream,\r\n    \"Constructed Writable from byte array with GetBuffer() enabled\");\r\n<\/pre>\n<p>And the properties display is:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Constructed Writable from byte array with GetBuffer() enabled\r\n--------------------------\r\nLength:             19\r\nCapacity:           19\r\nCanRead:            True\r\nCanSeek:            True\r\nCanWrite:           True\r\nCanTimeout:         False\r\npubliclyVisible:    True<\/pre>\n<p>The constructor attributes set the <code>Length<\/code>, <code>Capacity<\/code>, and <code>publiclyVisible<\/code> parameters as expected.<\/p>\n<h2>Writing to MemoryStream in C#<\/h2>\n<p>Once we have a <code>MemoryStream<\/code> object, we can use it to read, write and seek data in the system&#8217;s memory.<\/p>\n<p>Let&#8217;s see how we can write data to the <code>MemoryStream<\/code> object.<\/p>\n<p>First, let&#8217;s define the data we want to write:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var phrase1 = \"How to Use MemoryStream in C#\";\r\nvar phrase1Bytes = Encoding.UTF8.GetBytes(phrase1);\r\nvar phrase2 = \" - explanation with examples\";\r\nvar phrase2Bytes = Encoding.UTF8.GetBytes(phrase2);<\/pre>\n<p>We define two strings and the byte arrays created from these strings. Now let&#8217;s define our <code>MemoryStream<\/code> object and write byte arrays to it:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">memoryStream = Constructors.SimpleConstructor();\r\nMethods.WriteToMemoryStream(memoryStream, phrase1Bytes);\r\nMethods.WriteToMemoryStream(memoryStream, phrase2Bytes);\r\ndisplayProperties = Methods.ShowMemoryStreamProperties(memoryStream,\r\n    \"Writing to MemoryStream\");\r\n<\/pre>\n<p>Here are the properties of our <code>MemoryStream<\/code> object:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"raw\">Writing to MemoryStream\r\n--------------------------\r\nLength:             57\r\nCapacity:           256\r\nCanRead:            True\r\nCanSeek:            True\r\nCanWrite:           True\r\nCanTimeout:         False\r\npubliclyVisible:    True<\/pre>\n<p>Compared with the simple constructor example, we notice <code>Length<\/code> and <code>Capacity<\/code> parameters are changed after writing the data to the <code>MemoryStream<\/code> object.\u00a0<\/p>\n<p><strong>It is important to note this works only if the <\/strong><code>MemoryStream<\/code> <strong>does not have a set size. Attempt to write more data than the <\/strong><code>MemoryStream<\/code> <strong>object capacity will fail since the <\/strong><code>MemoryStream<\/code><strong> is not extensible:<\/strong><\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">[Fact]\r\npublic void WhenWritingOverTheCapacity_ThenFailure()\r\n{\r\n    var memoryStream = Constructors.ByteArrayConstructor(new byte[10]);\r\n    var addBytes = new byte[20];\r\n    Assert.Throws(() =&gt; memoryStream.Write(addBytes, 0, addBytes.Length));\r\n}\r\n<\/pre>\n<h2>Reading from MemoryStream in C#<\/h2>\n<p>To read data from the <code>MemoryStream<\/code>, we define a method <code>ReadFromMemoryStream()<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-highlight=\"6,11,16\">public static List ReadFromMemoryStream(MemoryStream memoryStream)\r\n{\r\n    var phrases = new List();\r\n\r\n    var buffer = new byte[10];\r\n    memoryStream.Position = 0;\r\n    memoryStream.Read(buffer, 0, 10);\r\n    phrases.Add(Encoding.UTF8.GetString(buffer));\r\n   \r\n    buffer = new byte[20];\r\n    memoryStream.Seek(10, SeekOrigin.Begin);\r\n    memoryStream.ReadAtLeast(buffer, 20);\r\n    phrases.Add(Encoding.UTF8.GetString(buffer));\r\n\r\n    buffer = new byte[27];\r\n    memoryStream.Seek(-27, SeekOrigin.End);\r\n    memoryStream.ReadExactly(buffer, 0, 27);\r\n    phrases.Add(Encoding.UTF8.GetString(buffer));\r\n\r\n    return phrases;\r\n}\r\n<\/pre>\n<p>Here we read data from the <code>MemoryStream<\/code> in parts using methods <code>Read()<\/code>, <code>ReadAtLeast()<\/code>, and <code>ReadExactly()<\/code>.\u00a0<\/p>\n<p><code>Read()<\/code> reads a block of bytes from the current stream, writes it to the buffer, and advances the position within the stream. It reads up to the specified number of bytes but does not block the stream if fewer bytes are available.\u00a0 It takes three parameters, <code>buffer<\/code> as a byte array, <code>offset<\/code> which defines at which position to begin storing data, and <code>count<\/code>, the maximum number of bytes to read.<\/p>\n<p><code>ReadAtLeast()<\/code> is an extension method and it takes two parameters, <code>buffer<\/code> as a <a href=\"https:\/\/code-maze.com\/csharp-bitarray\/\" target=\"_blank\" rel=\"noopener\">byte array<\/a> and <code>minimumBytes<\/code>. It reads at least <code>minimumBytes<\/code> from the current stream into the <code>buffer<\/code> and advances the position in the stream by the same amount. It will block the stream until the minimum number of bytes is available.<\/p>\n<p>Finally, <code>ReadExactly()<\/code> is also an extension method that reads exactly the required number of bytes, blocks the stream until all the bytes are available, and may throw an exception if the end of the stream is reached before all the bytes are read. It takes the same parameters as the <code>Read()<\/code> method.<\/p>\n<p>Highlighted lines show different ways to set the current position within the <code>MemoryStream<\/code> object.<\/p>\n<p>For example, to seek the data in the <code>MemoryStream<\/code>, we can set the <code>Position<\/code> property with the <code>Seek()<\/code> method. This method takes two parameters, an <code>Offset<\/code> and a <code>SeekOrigin<\/code>. The <code>Seek()<\/code> method will seek the specified offset from the specified <code>SeekOrigin<\/code> and return the new position.<\/p>\n<h2>Loading a File Using MemoryStream in C#<\/h2>\n<p>We often use <code>MemoryStream<\/code> when working with project resources.\u00a0<\/p>\n<p>Let&#8217;s use <code>MemoryStream<\/code> to load the image from the resource:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static void LoadImageFromResources()\r\n{\r\n    var imageMemoryStream = Constructors.ByteArrayConstructor(Resources.Image);\r\n    File.WriteAllBytes(\"Image.jpg\", imageMemoryStream.ToArray());\r\n}\r\n<\/pre>\n<p>After loading the image data to <code>MemoryStream<\/code>, we use it to save the image to the file system.<\/p>\n<h2>Serialization and Deserialization Using MemoryStream in C#<\/h2>\n<p>Another useful usage of <code>MemoryStream<\/code> is object serialization and deserialization.<\/p>\n<p>Let&#8217;s define a simple <code>Person<\/code> class:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public class Person\r\n{\r\n    public string FirstName { get; set; }\r\n    public string LastName { get; set; }\r\n    public int Age { get; set; }\r\n}<\/pre>\n<p>To serialize an object, we define a <code>SerializeObject()<\/code> method:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static byte[] SerializeObject(Person person)\r\n{\r\n    var memoryStream = Constructors.SimpleConstructor();\r\n    using var writer = new BinaryWriter(memoryStream);\r\n    writer.Write(person.FirstName);\r\n    writer.Write(person.LastName);\r\n    writer.Write(person.Age);\r\n\r\n    return memoryStream.ToArray();\r\n}\r\n<\/pre>\n<p>Here we create a <code>MemoryStream<\/code> object and populate it with the data from the <code>Person<\/code> instance using <code>BinaryWriter<\/code>.\u00a0<\/p>\n<p>Similarly, to deserialize this data to an object, we define a method <code>DeserializeObject()<\/code>:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">public static Person DeserializeObject(byte[] serializedData)\r\n{\r\n    Person deserializedPerson;\r\n    var memoryStream = Constructors.ByteArrayConstructor(serializedData);\r\n    using var reader = new BinaryReader(memoryStream);\r\n    deserializedPerson = new Person\r\n    {\r\n        FirstName = reader.ReadString(),\r\n        LastName = reader.ReadString(),\r\n        Age = reader.ReadInt32()\r\n    };\r\n\r\n    return deserializedPerson;\r\n}\r\n<\/pre>\n<p>Let&#8217;s see these methods in action:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">var person = new Person\r\n{\r\n    FirstName = \"Jack\",\r\n    LastName = \"Black\",\r\n    Age = 30\r\n};\r\nbyte[] serializedData = Methods.SerializeObject(person);\r\nvar deserializedPerson = Methods.DeserializeObject(serializedData);<\/pre>\n<h2>Conclusion<\/h2>\n<p>In this article, we learned about <code>MemoryStream<\/code> and analyzed its advantages over similar methods of working with the data. Furthermore, we saw how to use it to read, write and manipulate data in an easy, secure, and reliable way.<\/p>\n<p>In conclusion, if you are looking for an efficient and reliable way to handle data in your applications, <code>MemoryStream<\/code> is a great choice.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>MemoryStream in C# is a class that provides a stream implementation for in-memory data and offers several benefits over traditional file-based streams. This article will teach why, when, and how to use MemoryStream class in C#. Let&#8217;s dive in. What is MemoryStream? MemoryStream is a class in .NET that stores data in the system\u2019s memory. [&hellip;]<\/p>\n","protected":false},"author":45,"featured_media":62189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_et_pb_use_builder":"","_et_pb_old_content":"","_et_gb_content_width":"","footnotes":""},"categories":[12],"tags":[1701,1697,1699,1700,1698],"class_list":["post-84718","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-csharp","tag-loading-a-file-using-memorystream","tag-memorystream","tag-reading-from-memorystream","tag-serialization-and-deserialization-using-memorystream","tag-writing-to-memorystream","et-has-post-format-content","et_post_format-et-post-format-standard"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to Use MemoryStream in C# - Code Maze<\/title>\n<meta name=\"description\" content=\"In this article, we will learn what MemoryStream is in C# and how to use it to manipulate in-memory data in a secure and realiable way.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/code-maze.com\/csharp-memorystream\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Use MemoryStream in C# - Code Maze\" \/>\n<meta property=\"og:description\" content=\"In this article, we will learn what MemoryStream is in C# and how to use it to manipulate in-memory data in a secure and realiable way.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/code-maze.com\/csharp-memorystream\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Maze\" \/>\n<meta property=\"article:published_time\" content=\"2023-03-20T06:00:57+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-03-20T07:09:49+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1100\" \/>\n\t<meta property=\"og:image:height\" content=\"620\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Ivan Matec\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:site\" content=\"@CodeMazeBlog\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Ivan Matec\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"8 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":[\"Article\",\"BlogPosting\"],\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/\"},\"author\":{\"name\":\"Ivan Matec\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664\"},\"headline\":\"How to Use MemoryStream in C#\",\"datePublished\":\"2023-03-20T06:00:57+00:00\",\"dateModified\":\"2023-03-20T07:09:49+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/\"},\"wordCount\":1176,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"keywords\":[\"Loading a File Using MemoryStream\",\"MemoryStream\",\"Reading from MemoryStream\",\"Serialization and Deserialization Using MemoryStream\",\"Writing to MemoryStream\"],\"articleSection\":[\"C#\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/code-maze.com\/csharp-memorystream\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/\",\"url\":\"https:\/\/code-maze.com\/csharp-memorystream\/\",\"name\":\"How to Use MemoryStream in C# - Code Maze\",\"isPartOf\":{\"@id\":\"https:\/\/code-maze.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"datePublished\":\"2023-03-20T06:00:57+00:00\",\"dateModified\":\"2023-03-20T07:09:49+00:00\",\"description\":\"In this article, we will learn what MemoryStream is in C# and how to use it to manipulate in-memory data in a secure and realiable way.\",\"breadcrumb\":{\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/code-maze.com\/csharp-memorystream\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png\",\"width\":1100,\"height\":620,\"caption\":\"C# Development\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/code-maze.com\/csharp-memorystream\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/code-maze.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Use MemoryStream in C#\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/code-maze.com\/#website\",\"url\":\"https:\/\/code-maze.com\/\",\"name\":\"Code Maze\",\"description\":\"Learn. Code. Succeed.\",\"publisher\":{\"@id\":\"https:\/\/code-maze.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/code-maze.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/code-maze.com\/#organization\",\"name\":\"Code Maze\",\"url\":\"https:\/\/code-maze.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png\",\"width\":3511,\"height\":3510,\"caption\":\"Code Maze\"},\"image\":{\"@id\":\"https:\/\/code-maze.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/x.com\/CodeMazeBlog\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664\",\"name\":\"Ivan Matec\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/code-maze.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png\",\"contentUrl\":\"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png\",\"caption\":\"Ivan Matec\"},\"description\":\"Ivan has over twelve years of experience developing .NET and web applications. His experience includes developing several web-based solutions for medical institutions. Other projects include developing customized CRM and ERP solutions. Besides his every-day job, he enjoys experimenting with advanced software solutions (Microsoft Azure).\",\"sameAs\":[\"https:\/\/www.linkedin.com\/in\/ivan-matec\/\"],\"url\":\"https:\/\/code-maze.com\/author\/tecmaivan\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Use MemoryStream in C# - Code Maze","description":"In this article, we will learn what MemoryStream is in C# and how to use it to manipulate in-memory data in a secure and realiable way.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/code-maze.com\/csharp-memorystream\/","og_locale":"en_US","og_type":"article","og_title":"How to Use MemoryStream in C# - Code Maze","og_description":"In this article, we will learn what MemoryStream is in C# and how to use it to manipulate in-memory data in a secure and realiable way.","og_url":"https:\/\/code-maze.com\/csharp-memorystream\/","og_site_name":"Code Maze","article_published_time":"2023-03-20T06:00:57+00:00","article_modified_time":"2023-03-20T07:09:49+00:00","og_image":[{"width":1100,"height":620,"url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","type":"image\/png"}],"author":"Ivan Matec","twitter_card":"summary_large_image","twitter_creator":"@CodeMazeBlog","twitter_site":"@CodeMazeBlog","twitter_misc":{"Written by":"Ivan Matec","Est. reading time":"8 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["Article","BlogPosting"],"@id":"https:\/\/code-maze.com\/csharp-memorystream\/#article","isPartOf":{"@id":"https:\/\/code-maze.com\/csharp-memorystream\/"},"author":{"name":"Ivan Matec","@id":"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664"},"headline":"How to Use MemoryStream in C#","datePublished":"2023-03-20T06:00:57+00:00","dateModified":"2023-03-20T07:09:49+00:00","mainEntityOfPage":{"@id":"https:\/\/code-maze.com\/csharp-memorystream\/"},"wordCount":1176,"commentCount":0,"publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"image":{"@id":"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","keywords":["Loading a File Using MemoryStream","MemoryStream","Reading from MemoryStream","Serialization and Deserialization Using MemoryStream","Writing to MemoryStream"],"articleSection":["C#"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/code-maze.com\/csharp-memorystream\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/code-maze.com\/csharp-memorystream\/","url":"https:\/\/code-maze.com\/csharp-memorystream\/","name":"How to Use MemoryStream in C# - Code Maze","isPartOf":{"@id":"https:\/\/code-maze.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage"},"image":{"@id":"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage"},"thumbnailUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","datePublished":"2023-03-20T06:00:57+00:00","dateModified":"2023-03-20T07:09:49+00:00","description":"In this article, we will learn what MemoryStream is in C# and how to use it to manipulate in-memory data in a secure and realiable way.","breadcrumb":{"@id":"https:\/\/code-maze.com\/csharp-memorystream\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/code-maze.com\/csharp-memorystream\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/csharp-memorystream\/#primaryimage","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2021\/12\/social-csharp.png","width":1100,"height":620,"caption":"C# Development"},{"@type":"BreadcrumbList","@id":"https:\/\/code-maze.com\/csharp-memorystream\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/code-maze.com\/"},{"@type":"ListItem","position":2,"name":"How to Use MemoryStream in C#"}]},{"@type":"WebSite","@id":"https:\/\/code-maze.com\/#website","url":"https:\/\/code-maze.com\/","name":"Code Maze","description":"Learn. Code. Succeed.","publisher":{"@id":"https:\/\/code-maze.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/code-maze.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/code-maze.com\/#organization","name":"Code Maze","url":"https:\/\/code-maze.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2020\/01\/Code-Maze-Only-Logo-Transparent-HRez.png","width":3511,"height":3510,"caption":"Code Maze"},"image":{"@id":"https:\/\/code-maze.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/x.com\/CodeMazeBlog"]},{"@type":"Person","@id":"https:\/\/code-maze.com\/#\/schema\/person\/b35a6aa43a1c4795fd77e2519adfd664","name":"Ivan Matec","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/code-maze.com\/#\/schema\/person\/image\/","url":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png","contentUrl":"https:\/\/code-maze.com\/wp-content\/uploads\/2023\/12\/ivan-400x400-1-150x150.png","caption":"Ivan Matec"},"description":"Ivan has over twelve years of experience developing .NET and web applications. His experience includes developing several web-based solutions for medical institutions. Other projects include developing customized CRM and ERP solutions. Besides his every-day job, he enjoys experimenting with advanced software solutions (Microsoft Azure).","sameAs":["https:\/\/www.linkedin.com\/in\/ivan-matec\/"],"url":"https:\/\/code-maze.com\/author\/tecmaivan\/"}]}},"_links":{"self":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/84718","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/users\/45"}],"replies":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/comments?post=84718"}],"version-history":[{"count":6,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/84718\/revisions"}],"predecessor-version":[{"id":84725,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/posts\/84718\/revisions\/84725"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media\/62189"}],"wp:attachment":[{"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/media?parent=84718"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/categories?post=84718"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/code-maze.com\/wp-json\/wp\/v2\/tags?post=84718"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}