tags:

views:

520

answers:

6
+1  Q: 

C# using streams

Streams are kind of mysterious to me. I don't know when to use which stream and how to use them. Can someone explain to me how streams are used? If I understand correctly there are three stream types: stream, read stream and write stream. Is this correct? And what is for example the difference between a Memorystream and a FileStream?

A: 

Well you can both read and write to a stream in most cases, a memorystream is some thing you can declare so that you can work with data in memory and a file stream is a stream that is pointed to a file so when you write or read form a file stream then you are reading / writing the file.

Petoj
A: 

I would start by reading up on streams on MSDN: http://msdn.microsoft.com/en-us/library/system.io.stream.aspx

Memorystream and FileStream are streams used to work with raw memory and Files respectively...

Robban
A: 

There is only one basic type of Stream. However in various circumstances some members will throw an exception when called because in that context the operation was not available.

For example a MemoryStream is simply a way to moves bytes into and out of a chunk of memory. Hence you can call Read and Write on it.

On the other hand a FileStream allows you to read or write (or both) from/to a file. Whether you can actually Read or Write depends on how the file was opened. You can't Write to a file if you only opened it for Read access.

AnthonyWJones
A: 

I wouldn't call those different kind of streams. The Stream class have CanRead and CanWrite properties that tell you if the particular stream can be read from and written to.

The major difference between different stream classes (such as MemoryStream vs FileStream) is the backing store - where the data is read from or where it's written to. It's kind of obvious from the name. A MemoryStream stores the data in memory only, a FileStream is backed by a file on disk, a NetworkStream reads data from the network and so on.

Mattias S
A: 

Streams are good for dealing with large amounts of data. When it's impractical to load all the data into memory at the same time, you can open it as a stream and work with small chunks of it.

Krougan
+4  A: 

A stream is an object used to transfer data.Very often, the outside source will be a file , in this case you will use FileStream, but that is not necessarily the case. For example MemoryStream is used to store data in memory and System.Net.Sockets.NetworkStream handles network data.There is a generic stream class,System.IO.Stream, from which all streams are derived. Reader writer streams such as StreamReader and StreamWriter are not streams by their meaner, they are not derived from System.IO.Stream, they are designed to help to write and read data from and to stream!

ArsenMkrt
So, if i understand correctly, the stream contains the data and doesn't do anything with it. The reader and writer 'helper' classes can handle (manipulate) the data within the stream?
Martijn
No, Stream is not data container, it is using to transfer data, for example FileStream transfers data from byte[] to phisical file, NetworkStream transfers byte[] by socket. Reader Writer classes are helper classes to write and read from stream, for example StreamReader can be used to read from Stream string not byte[]. if you will give FileStream as a parameter it will read from File, if NetworkStream from socket.
ArsenMkrt