I try to count data with FileStream . It seems like everything looks fine, but as a result I get 5 records of the same array ( byte[] ) in tmp .

Apparently, I misunderstand the logic of reading from this stream. Tell me, what's the problem?

 using (FileStream fs = File.OpenRead(filePath)) { List<byte[]> tmp = new List<byte[]>(); while ((read = fs.Read(buffer, 0, buffer.Length)) > 0) { tmp.Add(buffer); } } 

    1 answer 1

    Arrays in C # reference type. So you add 5 links to the same array, which is refilled inside the loop. And after the end of the cycle, you have in the list 5 references to the same array with the last value received. Move the reading and creation of the array into the loop:

      while (true) { var buffer=new int[size]; read = fs.Read(buffer, 0, buffer.Length); if (read>0) tmp.Add(buffer); else break; } 
    • Thank! It turns out everything was just) - viton-zizu