Quite a number of .NET APIs take streams as parameters.

However, you will typically run into a challenge where your data is not a stream, and you will typically need to do some gymnastics to convert it into one.

Take this example:

// Get our data
var data =
"""
The quick brown fox
mightily jumped over
the brown dog very
very bigly
""";

// Convert our data a byte array
var dataInBytes = Encoding.Default.GetBytes(data);

// Convert our byte array into a memory stream
var dataStream = new MemoryStream(dataInBytes);

You will see here that we need to create an intermediate byte array, and then convert that into a stream.

This is such a common use case that .NET 11 now has a solution to this: the StringStream. (As I write this, the official API documentation is yet to be updated, for Preview 6)

Our code above can be rewritten as follows:

// Directly create a stream
var directDataStream = new StringStream(data, Encoding.UTF8);

Of importance here is that you have to specify the encoding of your string, which is typically UTF8. Unless you really know what you’re doing, specify Encoding.UTF8.

TLDR

.NET 11 has new APIs, such as the StringStream, that allow you to directly create Streams from text.

The code is in my GitHub.

Happy hacking!