["","","37 C# ","A(wsyzXB(XBBST~~}T~~YF64G5766F+CE*XS}T~~}T~~","MemoryStream"," represents a pure, in-memory stream of data. It is found in the System.IO namespace. It is derived from the Stream type. It is useful when using BinaryReader. It can be reset\u2014this leads to performance improvements. ","Stream ","stream","First,"," let's examine this program from a higher level. The program physically reads in the bytes of specified file into the computer's memory. No more disk accesses occur after this.","A MemoryStream is constructed"," from this byte array containing the file's data. Then, the MemoryStream is used as a backing store for the BinaryReader type, which acts upon the in-memory representation. ","Byte Array ","byte-array","BinaryReader ","binaryreader","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto","Based on:"," .NET 4.7 (2017)\n\n","C# program that uses the MemoryStream type","\n\nusing System;\nusing System.IO;\n\nclass Program\n{\n static void Main()\n {","\n // Read all bytes in from a file on the disk.\n ","byte[] file = File.ReadAllBytes(","\"C:\\\\ICON1.png\"",");","\n\n // Create a memory stream from those bytes.\n ","using"," (","MemoryStream"," memory = new MemoryStream(file))\n {","\n // Use the memory stream in a binary reader.\n ","using (BinaryReader reader = new BinaryReader(memory))\n {","\n // Read in each byte from memory.\n ","for (int i = 0; i < file.Length; i++)\n {\n byte result = reader.ReadByte();\n Console.WriteLine(result);\n }\n }\n }\n }\n}\n\n","Output","\n\n137\n80\n78\n71\n13\n10\n26\n10\n0\n0\n0\n13\n73\n72...","Discussion."," It sometimes helps to put data into memory and simply leave it there. Memory is much faster than disk or network accesses. With MemoryStream, we can act upon the byte array stored in memory rather than a file or other resource. ","Note: ","This consolidates resource acquisitions. It also gives you the ability to reliably use multiple streams on a single piece of data.","Memory Hierarchy ","memory-hierarchy","Also,"," you can sometimes reuse a single MemoryStream. Store the MemoryStream instance as a field. Then call the SetLength(0) method on the MemoryStream instance to reset it. This will reduce allocations during the algorithm.","Summary."," MemoryStream allows you to use in-memory byte arrays or other data as though they are streams. Instead of storing data in files, you can store data in-memory for additional performance and control over the behavior of your program. ","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","3679700504","data-ad-format","link","ins","class","adsbygoogle","data-ad-client","ca-pub-4712093147740724","data-ad-slot","6227126509","data-ad-format","auto"]

%iVBORw0KG;)NSUhEUg?ANM)7CAM?ADWzk8E)MFBMVEX::79fz47vv+/P704vj25/nx2PbFyvHj5filruvz3ffT1/WKl+Z6iePw1fWapemLxEvf?AEGklEQVR4Xu3aa3ODKBQG4HPjrub:9stajyLhFbYZKfN5P1UBh15gCI4gV8SRmR4k3Bc7sH3I9GH9CF9SB/Sv2M+pL8QPPI0wCeM93BVrsNIRtYQIX87SNysQJJ1EYmCoGk/iI+bB3cw3yxZbOJSxApdWB60os5jFOqDipixVdc0SaQgTWySzBWSwjVMCioT+bkkjO2njJMWrEV2eRXJlHZZXkKyDI2uezmJ7GtIC0HVda8nKfUVpMjNQbLxJaSavgiRSBwnRUNotYit2SAIIHrZV15FokOBX+knmXwLq4lKksqzFavmjJLsV8zIFptKUrtCfpzgkulor846TUXaQW2SJX4CScvSpBcki0NbZyXFGKm6ThMN8bNIkRsT76mkLJLWHG81ut3SmtAmYfHGYqPCAZIpSHIigV3q0PNJbBWBaGwhHydlk5jkc4ruq0LDJGyQmi8wM0TSR+dhwjkndJi6SLZB4sciS9BF0hUOyO4mITiZ2JZZTXwi7XVDJMXXEb5O0hVO27SaCNSk+35NVtX79aNKrpIsws+miHA5tIp0OUCJMaNymwpTfeBcVeZEulfFAVJ7Ly4I3SQRs5OMEdFuVlMRJDL5wqrlaPaIGSWxOXsMMXQE7b7A7SQmog0l/MMnCyKqWs601xCNknSQ6MoXB5eTciG5HACO1q4kohSmeZ6nEFyye:rLZwFiPfyeu0U/Knl3rkQgnPeJ+fSGIk69ws6kcKcA2yyKUZDdJs1zkgm6S1sJG70uYgrRKGoC0MknXcIYyaU1SRmI6nJcHELNkwzgZKmuWVCe52kOb6w9ZnMZjLkyu4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