Here we open the same binary file and then read in the bytes. BinaryReader here provides some useful properties. When you run the next program, you will see all the ints recovered.
Note We see that BinaryReader receives a FileStream. This is returned by File.Open.
Info The program calls ReadInt32. This method consumes 4 bytes in the binary file and turns it into an int.
Also The method advances the file read. It increments our position. This line adds 4 bytes to our position.
Here We use sizeof int. This evaluates to the integer value 4 and always will, but it might be preferable for clarity.
static void Main()
static void R()
using (BinaryReader b = new BinaryReader
// Position and length variables.
int pos = 0;
// Use BaseStream.
int length = (int)b.BaseStream.Length;
while (pos < length)
// Read integer.
int v = b.ReadInt32
// Advance our position variable.
pos += sizeof(int);