What is the difference between Read() and ReadBlock() calls on TextReader?
c#, stream, streamreader, textreader
Solution
If you can do something useful with a partial result, then call Read() and work on what you get. In particular if you are looping through and working on the result of each Read() then do this rather than with ReadBlock().
The word 'block' in ReadBlock refers to the action of blocking, not to the next 'block' of bytes. So it will force to stop further execution until it completed.
If you use Reflector to look at the implementation of StreamReader.ReadBlock (which is inherited from TextReader.ReadBlock), all it does is make multiple calls to the "Read" method until either the "Read" method returns 0 or we have read as many bytes as requested. This is needed because the "Read" method will not necessarily return as many bytes as you asked for.
Problem
The title more or less says it all. The calls are documented: Here for `TextReader.Read Method (Char[], Int32, Int32)` and Here for `TextReader.ReadBlock()` with the same argument types. I want to extract a portion of a byte array, for which I make up a `MemoryStream` that I intent to read with a `StreamReader` like so: ``` StreamReader r = new StreamReader(new MemoryStream(rawData, 0, 184, false, false)); r.Read..... ``` Which one should I use? Is one of them faster or otherwise better?