Generating a stream from a string is a common operation we perform when building applications. In this article, we will discuss two ways we can achieve that in .NET. 

To download the source code for this article, you can visit our GitHub repository.

Let’s dive in.

Use the MemoryStream and StreamWriter Classes

First of all, let’s create a GetStreamWithStreamWriter() method that utilizes the MemoryStream and StreamWriter Classes. This method allows us to create a MemoryStream object and use a StreamWriter to write our string to it:

Support Code Maze on Patreon to get rid of ads and get the best discounts on our products!
Become a patron at Patreon!
public static Stream GetStreamWithStreamWriter(string sampleString, Encoding? encoding = null)
{
    encoding ??= Encoding.UTF8;

    var stream = new MemoryStream(encoding.GetByteCount(sampleString));
    using var writer = new StreamWriter(stream, encoding, -1, true);
    writer.Write(sampleString);
    writer.Flush();
    stream.Position = 0;

    return stream;
}

This method has two parameters. The first parameter is our string, while the second parameter is an optional character encoding instance.

By default, this method utilizes the UTF-8 encoding. However, we can specify a different encoding when invoking the method.

Within the method, we begin by creating a MemoryStream object with an initial capacity based on the byte count of the sample string using the encoding we specify. Then, we initialize a StreamWriter object, associating it with the MemoryStream and the specified encoding.

Also, we use -1 as a buffer size to use the default buffer size of 1024 bytes.  We also configure the StreamWriter to leave the MemoryStream open after it’s disposed of with the using statement, by passing true as the last argument.

Here, we use the StreamWriter instance, which serves as a convenient wrapper to the underlying stream API, facilitating the writing of strings to various stream types beyond just MemoryStream. This is useful when we want to write the string to different stream types. Additionally, it enables us to lazy-write to the stream through consecutive calls to the Write() method.

Next, we write the contents of the string to the MemoryStream using the writer.Write() method. Then, we invoke the writer.Flush() method to be certain that all the buffered data is written to the stream.

After completing the writing process, we reset the position of the stream to 0. This allows subsequent operations to begin reading from the beginning of the stream.

Finally, we return the MemoryStream object.

Use the GetBytes Method and MemoryStream Class

Now, let’s explore an alternative approach to convert a string to a stream:

public static Stream GetStreamWithGetBytes(string sampleString, Encoding? encoding = null)
{
    encoding ??= Encoding.UTF8;

    var byteArray = encoding.GetBytes(sampleString);
    var memoryStream = new MemoryStream(byteArray);

    return memoryStream;
}

Here, we pass our sample string into the Encoding.GetBytes() method to generate a byte array. Subsequently, we use this byte array to initialize a new MemoryStream object and return it.

Performance of Generating a Stream From a String?

We now have two methods of generating a stream from a string. Let’s see which method is the fastest in generating a stream from a string using the BenchmarkDotNet library.

Now, let’s execute our benchmark method in the Program class and inspect the results on the console:

|                         Method |      Mean |     Error |    StdDev |    Gen0 | Allocated |
|------------------------------- |----------:|----------:|----------:|--------:|----------:|
|     UseGetBytesAndMemoryStream |  3.780 us | 0.0757 us | 0.1286 us | 12.6572 |   6.51 KB |
| UseMemoryStreamAndStreamWriter | 19.489 us | 0.8328 us | 2.4557 us | 47.6074 |  24.57 KB |

From our benchmark results, we can see that the UseGetBytesAndMemoryStream() method is the faster way of generating a Stream from a String in C#. Additionally, this method utilizes less memory for the operation.

However, there is only a slight difference in the execution time of these two methods. This suggests that in production, any of these methods can work efficiently unless we have a need for micro-optimizations.

Conclusion

In this article, we have covered two methods of generating a stream from a string in C#. Furthermore, based on our benchmark results, we found out that one method is faster and more memory efficient than the other one.

Liked it? Take a second to support Code Maze on Patreon and get the ad free reading experience!
Become a patron at Patreon!