I have a potentially larger int
array that I'm writing to a file using BinaryWriter
. Of course, I can use the default approach.
using (BinaryWriter writer = new BinaryWriter(File.Open(path, FileMode.Create)))
{
writer.Write(myIntArray.Length);
foreach (int value in myIntArray)
writer.Write(value);
}
But this seems horribly inefficient. I'm pretty sure an int
array stores data contiguously in memory. Is there no way to just write the memory directly to a file like you can with a byte
array? Maybe a way to cast (not copy) the int
array to a byte
array?