C# – How to convert an Stream into a byte[] in C#?

bytearrayc++inputstream

Is there a simple way or method to convert an Stream into a byte[] in C#?

Best Solution

The shortest solution I know:

using(var memoryStream = new MemoryStream())
{
  sourceStream.CopyTo(memoryStream);
  return memoryStream.ToArray();
}