How to create an instance of System.IO.Stream stream

C#MemoryStream

C# Problem Overview


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 ?

C# Solutions


Solution 1 - C#

System.IO.Stream stream = new System.IO.MemoryStream();

Solution 2 - C#

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.

Solution 3 - C#

Stream stream = new MemoryStream();

you can use MemoryStream

Reference: [MemoryStream][1]

[1]: https://msdn.microsoft.com/en-us/library/system.io.memorystream(v=vs.110).aspx "MemoryStream"

Solution 4 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionRaufView Question on Stackoverflow
Solution 1 - C#RaufView Answer on Stackoverflow
Solution 2 - C#Dylan SmithView Answer on Stackoverflow
Solution 3 - C#ojlovecdView Answer on Stackoverflow
Solution 4 - C#Bradley UffnerView Answer on Stackoverflow