What is the purpose of StreamReader when Stream.Read() exists?

前端 未结 3 1087
悲&欢浪女
悲&欢浪女 2020-12-15 23:48

This has been bugging me. I know Stream is an abstract class and therefore can\'t be instantiated but it has classes that are derived from it. Why is there

3条回答
  •  无人及你
    2020-12-16 00:32

    A StreamReader is a TextReader which means it is a Stream wrapper. A TextReader will convert (or encode) Text data (string or char) to byte[] and write them down to the underlying Stream.

    Looking at the difference between the two implementations, you can see that StreamReader derives from TextReader, which, as declared, deals with text instead of bytes. It seems to me as an abstraction for users who want to work with textual representation. Of course, the underlying implementation will need a Stream to consume such data, but will provide a level of abstraction for end users.

提交回复
热议问题