Stream.Seek(0, SeekOrigin.Begin) or Position = 0

匿名 (未验证) 提交于 2019-12-03 01:49:02

问题:

When you need to reset a stream to beginning (e.g. MemoryStream) is it best practice to use

stream.Seek(0, SeekOrigin.Begin); 

or

stream.Position = 0; 

I've seen both work fine, but wondered if one was more correct than the other?

回答1:

Use Position when setting an absolute position and Seek when setting a relative position. Both are provided for convenience so you can choose one that fits the style and readability of your code. Accessing Position requires the stream be seekable so they're safely interchangeable.



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