I need to write bytes of an IEnumerable<byte> to a file.
I can convert it to an array and use Write(byte[]) method:
using (var stream = File.Create(path))
stream.Write(bytes.ToArray());
But since IEnumerable doesn't provide the collection's item count, using ToArray is not recommended unless it's absolutely necessary.
So I can just iterate the IEnumerable and use WriteByte(byte) in each iteration:
using (var stream = File.Create(path))
foreach (var b in bytes)
stream.WriteByte(b);
I wonder which one will be faster when writing lots of data.
I guess using Write(byte[]) sets the buffer according to the array size so it would be faster when it comes to arrays.
My question is when I just have an IEnumerable<byte> that has MBs of data, which approach is better? Converting it to an array and call Write(byte[]) or iterating it and call WriteByte(byte) for each?