80

How to create an instance of System.IO.Stream stream. One of my function receives System.IO.Stream stream as parameter and write some thing to it. So how can I create a new instance of the same and pass it to the function ?

Rauf
  • 12,326
  • 20
  • 77
  • 126

4 Answers4

135
System.IO.Stream stream = new System.IO.MemoryStream();
Rauf
  • 12,326
  • 20
  • 77
  • 126
25

You have to create an instance of one of the subclasses. Stream is an abstract class that can't be instantiated directly.

There are a bunch of choices if you look at the bottom of the reference here:

Stream Class | Microsoft Developer Network

The most common probably being FileStream or MemoryStream. Basically, you need to decide where you wish the data backing your stream to come from, then create an instance of the appropriate subclass.

Dylan Smith
  • 22,069
  • 2
  • 47
  • 62
25
Stream stream = new MemoryStream();

you can use MemoryStream

Reference: MemoryStream

Ritwick Dey
  • 18,464
  • 3
  • 24
  • 37
ojlovecd
  • 4,812
  • 1
  • 20
  • 22
2

Stream is a base class, you need to create one of the specific types of streams, such as MemoryStream.

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76