Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference of Stream and MemoryStream

Tags:

c#

What is the main difference between Stream and MemoryStream in C#?

If I need to create a Stream without a file shall I use a MemoryStream instead?

like image 654
Mahib Avatar asked May 06 '14 09:05

Mahib


People also ask

What is a MemoryStream?

MemoryStream encapsulates data stored as an unsigned byte array. The encapsulated data is directly accessible in memory. Memory streams can reduce the need for temporary buffers and files in an application. The current position of a stream is the position at which the next read or write operation takes place.

What is meant by MemoryStream in C#?

The MemoryStream class creates streams that have memory as a backing store instead of a disk or a network connection. MemoryStream encapsulates data stored as an unsigned byte array that is initialized upon creation of a MemoryStream object, or the array can be created as empty.

How do I use MemoryStream?

This code shows how to use MemoryStream class and its member to read data in memory stream, which can be used to save it from there. int count; //GetByteData function to get Byte data like if you fetch Image column data from sqlserver or somewhere. byte[] byteArray = getByteData();

What is the difference between a stream and a byte array?

The major difference between these is that the input/output stream classes read/write byte stream data. Whereas the Reader/Writer classes handle characters. The methods of input/output stream classes accept byte array as parameter whereas the Reader/Writer classes accept character array as parameter.


1 Answers

MemoryStream derives from the general class Stream.

About Stream from MSDN:

Provides a generic view of a sequence of bytes. This is an abstract class.

So you can't create an instance of Stream. You have to use one of the derived classes, like MemoryStream, FileStream, etc.

like image 154
Patrick Hofman Avatar answered Sep 20 '22 15:09

Patrick Hofman