Modernized and slightly modified version of the extension methods for ToStream:
public static Stream ToStream(this string value) => ToStream(value, Encoding.UTF8); public static Stream ToStream(this string value, Encoding encoding) => new MemoryStream(encoding.GetBytes(value ?? string.Empty)); Modification as suggested in @Palec's comment of @Shaun Bowe answer.
Or as a one-liner (suggested by @satnhak):
public static Stream ToStream(this string value, Encoding encoding = null) => new MemoryStream((encoding ?? Encoding.UTF8).GetBytes(value ?? string.Empty));