How to convert byte array to an object stream in C#?

A stream is an abstract base class that provides a generic view of a sequence of bytes. Streams support three fundamental operations: reading, writing, and seeking. Converting a byte array to a stream allows you to work with the data using stream-based operations and can lead to performance improvements in certain scenarios.

In C#, you can convert a byte array to an object stream using the MemoryStream class, which represents a stream whose backing store is memory.

Syntax

Following is the syntax for converting a byte array to a MemoryStream −

MemoryStream stream = new MemoryStream(byteArray);

You can also specify additional parameters for more control −

MemoryStream stream = new MemoryStream(byteArray, writable: false);
MemoryStream stream = new MemoryStream(byteArray, index: 0, count: byteArray.Length);

Byte Array to MemoryStream Conversion Byte Array [1, 2, 3, 4, 5] Raw bytes in memory new MemoryStream() MemoryStream Stream object Read/Write/Seek operations Stream provides sequential access to byte data

Using MemoryStream with BinaryReader

This example demonstrates converting a byte array to a MemoryStream and reading the data using a BinaryReader −

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      byte[] byteArray = new byte[5] {1, 2, 3, 4, 5 };
      
      using (MemoryStream stream = new MemoryStream(byteArray)) {
         using (BinaryReader reader = new BinaryReader(stream)) {
            Console.WriteLine("Reading bytes from MemoryStream:");
            for (int i = 0; i < byteArray.Length; i++) {
               byte result = reader.ReadByte();
               Console.WriteLine($"Byte {i + 1}: {result}");
            }
         }
      }
   }
}

The output of the above code is −

Reading bytes from MemoryStream:
Byte 1: 1
Byte 2: 2
Byte 3: 3
Byte 4: 4
Byte 5: 5

Using MemoryStream for Direct Reading

You can also read directly from the MemoryStream without using a BinaryReader −

using System;
using System.IO;

class Program {
   static void Main(string[] args) {
      byte[] byteArray = {10, 20, 30, 40, 50};
      
      using (MemoryStream stream = new MemoryStream(byteArray)) {
         Console.WriteLine("Stream Length: " + stream.Length);
         Console.WriteLine("Stream Position: " + stream.Position);
         
         int byteRead;
         Console.WriteLine("Reading bytes directly from stream:");
         while ((byteRead = stream.ReadByte()) != -1) {
            Console.WriteLine("Value: " + byteRead + ", Position: " + stream.Position);
         }
      }
   }
}

The output of the above code is −

Stream Length: 5
Stream Position: 0
Reading bytes directly from stream:
Value: 10, Position: 1
Value: 20, Position: 2
Value: 30, Position: 3
Value: 40, Position: 4
Value: 50, Position: 5

Converting String to Byte Array and Stream

This example shows how to convert a string to a byte array and then to a MemoryStream −

using System;
using System.IO;
using System.Text;

class Program {
   static void Main(string[] args) {
      string text = "Hello World!";
      byte[] byteArray = Encoding.UTF8.GetBytes(text);
      
      using (MemoryStream stream = new MemoryStream(byteArray)) {
         using (StreamReader reader = new StreamReader(stream)) {
            Console.WriteLine("Original text: " + text);
            Console.WriteLine("Byte array length: " + byteArray.Length);
            Console.WriteLine("Stream content: " + reader.ReadToEnd());
         }
      }
   }
}

The output of the above code is −

Original text: Hello World!
Byte array length: 12
Stream content: Hello World!

Conclusion

Converting a byte array to a MemoryStream in C# is accomplished using the MemoryStream constructor. This conversion enables you to use stream-based operations like reading, writing, and seeking on byte data, providing flexibility in data manipulation and improved performance for certain operations.

Updated on: 2026-03-17T07:04:36+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements