Make an extension method over StringBuilder.AppendLine with specific FormatProvider
In order to avoid using the code which always explicitly passes FormatProvider
StringBuilder sb = new ();
var invariant = CultureInfo.InvariantCulture;
sb.AppendLine(invariant, $"Now: {DateTime.UtcNow:yyyy.MM.dd HH:mm}");
, decided to make an extension method, to be able change the code above to the next view
StringBuilder sb = new ();
sb.AppendInvariantLine($"Now: {DateTime.UtcNow:yyyy.MM.dd HH:mm}");
, but due to lack of understanding of interpolated string internal implementation, was not able to do it.
My best was
public static StringBuilder AppendInvariantLine(
this StringBuilder stringBuilder,
ref StringBuilder.AppendInterpolatedStringHandler handler)
{
return stringBuilder.AppendLine(CultureInfo.InvariantCulture, ref handler);
}
, but it is even not callable, let alone correctness of the implimentation.
The compiler error with this call sb.AppendInvariantLine($"Now: {DateTime.UtcNow:yyyy.MM.dd HH:mm}")
:
CS1615: Argument 3 may not be passed with the ‘out’ keyword