开发者

Relationship between BinaryFormatter and Filestream

开发者 https://www.devze.com 2023-04-05 20:06 出处:网络
New to .net but still confuse about the concept of BinaryFormatter and Filestream, from all i read 开发者_如何学Pythonthey both seem to be doing the same thing or similar concept. Ex. is Binaryformatt

New to .net but still confuse about the concept of BinaryFormatter and Filestream, from all i read 开发者_如何学Pythonthey both seem to be doing the same thing or similar concept. Ex. is Binaryformatter.serialize, how can that incorporate filestream and the object as parameter while i thought filestream function was to transfer the stream object to the file. I'm just confuse, can someone tell me how they work together and the difference between the two?


Streams represent raw data that can be accessed sequentially. They are used (directly or indirectly) whenever you input or output. There are different kinds of streams. For example:

  • NetworkStream and FileStream read and write data from a network port and disk without performing any transformations.
  • GzipStream or CryptoStream decorate an underlying stream by adding compression and encryption.

BinaryFormatter requires a Stream to write or read from. But its responsibility is very different: it's used to convert .NET objects to sequence of bytes that can saved or transmitted through a network. Concrete medium and additional transformations are determined by the type of stream you use.

All streams inherit from the Stream class and share the same interface which is very convenient. Classes like BinaryFormatter can rely on this shared interface without knowing the specifics of particular implementation.

Once again, BinaryFormatter is for converting an object to and from a sequence of bytes. Streams are for reading and writing these bytes to a particular medium.

0

精彩评论

暂无评论...
验证码 换一张
取 消