Skip to content

Instantly share code, notes, and snippets.

@BoolPurist
Created July 2, 2022 18:56
Show Gist options
  • Save BoolPurist/b89c4cf77a767a944052010e9491a2e3 to your computer and use it in GitHub Desktop.
Save BoolPurist/b89c4cf77a767a944052010e9491a2e3 to your computer and use it in GitHub Desktop.
Extension to get string readable string representation for any sequence.
using System.Text;
namespace <SomeNamespace>;
public static class AsTextExtension
{
public static string AsText<T>(this IEnumerable<T> sequence, string separator = ", ", int numberOfBreak = 50)
{
var builder = new StringBuilder();
builder.Append("[ ");
int counterForLineBreak = 0;
foreach (var element in sequence)
{
counterForLineBreak++;
builder.Append($"{element}{separator}");
if (counterForLineBreak > numberOfBreak)
{
builder.AppendLine();
counterForLineBreak = 0;
}
}
builder.Remove(builder.Length - separator.Length, separator.Length);
builder.Append(" ]");
return builder.ToString();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment