One way to avoid creating a new array byte[]for each call is to create it BinaryWriteron top MemoryStream, write integers to it, and then collect all the results at once by accessing the MemoryStreambuffer:
var buf = new byte[400];
using (var ms = new MemoryStream(buf))
using (var bw = new BinaryWriter(ms)) {
for (int i = 0 ; i != 100 ; i++) {
bw.Write(2*i+3);
}
}
source
share