Skip to main content
added 225 characters in body
Source Link
Nick N.
  • 13.7k
  • 9
  • 62
  • 77

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)); 

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.

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)); 
deleted 53 characters in body
Source Link
Nick N.
  • 13.7k
  • 9
  • 62
  • 77

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)); 

It wasModification as suggested in a@Palec's comment of @JoelNet's answer and @Shaun Bowe answer. Modification as suggested in @Palec's comment.

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)); 

It was suggested in a comment of @JoelNet's answer and @Shaun Bowe answer. Modification as suggested in @Palec's comment.

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.

added 22 characters in body
Source Link
Nick N.
  • 13.7k
  • 9
  • 62
  • 77

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)); 

It was suggested in a comment of @JoelNet's answer and @Shaun Bowe answer. Because I agree withModification as suggested in @Palec's comment.

Modernized 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)); 

It was suggested in a comment of @JoelNet's answer and @Shaun Bowe answer. Because I agree with @Palec's comment.

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)); 

It was suggested in a comment of @JoelNet's answer and @Shaun Bowe answer. Modification as suggested in @Palec's comment.

added 7 characters in body
Source Link
ΩmegaMan
  • 32.2k
  • 13
  • 110
  • 138
Loading
deleted 5 characters in body
Source Link
ΩmegaMan
  • 32.2k
  • 13
  • 110
  • 138
Loading
Readbility w/o scrolling.
Source Link
ΩmegaMan
  • 32.2k
  • 13
  • 110
  • 138
Loading
Source Link
Nick N.
  • 13.7k
  • 9
  • 62
  • 77
Loading