C# programming
Data Conversion
Software Development
ReadOnlyMemory
Programming Arrays

C# convert ReadOnlyMemory<byte> to byte[]

Codemia Knowledge Hub ·

Call ToArray(). It always returns a new byte[] copy and is the safest general answer. Zero-copy access to an underlying array is possible only in some cases and needs more care.

ToArray(): the safe default

If you need a real byte array and an allocated copy is fine, use ToArray():

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        ReadOnlyMemory<byte> memory = new byte[] { 10, 20, 30, 40 };
8        byte[] bytes = memory.ToArray();
9
10        Console.WriteLine(string.Join(", ", bytes));
11    }
12}

This always creates a new byte[], so:

  • it is safe
  • it works no matter how the memory was created
  • changes to the new array do not affect the original source

For most application code, this is the right default.

Why the conversion is not free

ReadOnlyMemory<byte> lets you pass byte data around without forcing copies. It can represent:

  • a full array
  • a slice of an array
  • memory from other sources exposed through the memory abstractions

A byte[] is one concrete owned buffer, while ReadOnlyMemory<byte> is more general. That is why converting may require a copy.

Zero-copy access with MemoryMarshal.TryGetArray

If the memory is backed by an array, MemoryMarshal.TryGetArray can give you that underlying array:

csharp
1using System;
2using System.Runtime.InteropServices;
3
4class Program
5{
6    static void Main()
7    {
8        ReadOnlyMemory<byte> memory = new byte[] { 1, 2, 3, 4, 5 };
9
10        if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
11        {
12            Console.WriteLine(segment.Array != null);
13            Console.WriteLine(segment.Offset);
14            Console.WriteLine(segment.Count);
15        }
16    }
17}

The returned segment.Array is not guaranteed to be exactly the logical slice you want. You have to respect Offset and Count, so treating segment.Array as the answer is wrong when the memory is a slice of a larger array.

Handling slices correctly

For example:

csharp
1using System;
2using System.Runtime.InteropServices;
3
4class Program
5{
6    static void Main()
7    {
8        byte[] source = { 10, 20, 30, 40, 50 };
9        ReadOnlyMemory<byte> memory = source.AsMemory(1, 3);
10
11        if (MemoryMarshal.TryGetArray(memory, out ArraySegment<byte> segment))
12        {
13            Console.WriteLine(string.Join(", ", segment.Array!));
14            Console.WriteLine($"Offset={segment.Offset}, Count={segment.Count}");
15        }
16    }
17}

The underlying array is still the full source, not the three-byte slice. If you need a standalone array for the slice, ToArray() is still the cleanest choice.

When not to convert at all

Many modern .NET APIs accept ReadOnlyMemory<byte> or ReadOnlySpan<byte>. If the next API can consume memory directly, keeping the existing type avoids an allocation. This matters in:

  • high-throughput networking
  • parsing pipelines
  • serialization code
  • streaming systems

The need to convert usually comes from an older API boundary, not from the data itself.

Copying into a preallocated array

To control allocation, create the destination yourself and copy into it:

csharp
1using System;
2
3class Program
4{
5    static void Main()
6    {
7        ReadOnlyMemory<byte> memory = new byte[] { 5, 6, 7 };
8        byte[] destination = new byte[memory.Length];
9
10        memory.Span.CopyTo(destination);
11        Console.WriteLine(string.Join(", ", destination));
12    }
13}

This still copies, but you decide where the destination buffer comes from.

Mistakes to avoid

  • Assuming TryGetArray returns a perfectly sized array. It may expose a larger array plus an offset and count.
  • Calling ToArray() in hot paths without measuring allocations. It is correct, but may cost more than necessary if the next API already accepts spans or memory.
  • Treating read-only as immutable. ReadOnlyMemory<byte> being read-only does not mean the underlying array cannot change elsewhere. Read-only describes the view, not the data.

Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms