What are the default values for StreamReader?

故事扮演 提交于 2019-12-08 16:07:31

问题


I need to use this constructor public StreamReader(Stream stream, Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize, bool leaveOpen) in order to set leaveOpen to true. And in order to do that I need to set the other parameters as well (Encoding encoding, bool detectEncodingFromByteOrderMarks, int bufferSize). I want to use StreamReader as it is so I do not want to give some random values. What are the default values for these parameters?

By the way, I know that I can use StreamReader without using. And GC will get rid of it without touching the stream.


回答1:


The default values are the following:

  • Encoding: Encoding.UTF8
  • detectEncodingFromByteOrderMarks: true
  • DefaultBufferSize: 1024

You can see the constructors and the values yourself by visiting Reference Source




回答2:


Encoding.UTF8, true, and 1024, respectively.

Source: the source.

That the default for Encoding is Encoding.UTF8 and the default for bufferSize is 1024 is also documented in the MSDN, but the default for detectEncodingFromByteOrderMarks doesn't appear to be.




回答3:


You can inspect the reference source to determine this.

It reveals that:

  • detectEncodingFromByteOrderMarks is true
  • DefaultBufferSize is 1024 or 4096
  • encoding is Encoding.UTF8
  • leaveOpen is false

Personally, I'd make the DefaultBufferSize 4096 for a desktop app.




回答4:


Stream stream you need to pass to read from. Encoding encoding default AFAIK is UTF-8. int bufferSize is minimum 128 characters. bool leaveOpen is false by default and so it will call Dispose() after reading the stream

Documentation said it clearly.



来源:https://stackoverflow.com/questions/38584527/what-are-the-default-values-for-streamreader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!