This is another pretty basic class I wrote for a library as I hate the way the default StringBuilder in .NET works.
Essentially, I wanted to have the + operator, as well as implicit conversions to strings. (Rather than needing .ToString() all the time.)
It's pretty small and simple, so there may not be a lot to critique.
Also, before you say "just inherit StringBuilder and extend it", it's sealed.
/// <summary>
/// This wraps the .NET <code>StringBuilder</code> in a slightly more easy-to-use format.
/// </summary>
public class ExtendedStringBuilder
{
private StringBuilder _stringBuilder;
public string CurrentString => _stringBuilder.ToString();
public int Length => _stringBuilder.Length;
public ExtendedStringBuilder()
{
_stringBuilder = new StringBuilder();
}
public ExtendedStringBuilder(int capacity)
{
_stringBuilder = new StringBuilder(capacity);
}
public ExtendedStringBuilder Append(string s)
{
_stringBuilder.Append(s);
return this;
}
public ExtendedStringBuilder Append(char c)
{
_stringBuilder.Append(c);
return this;
}
public ExtendedStringBuilder Append(object o)
{
_stringBuilder.Append(o);
return this;
}
public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, string s) => sb.Append(s);
public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, char c) => sb.Append(c);
public static ExtendedStringBuilder operator +(ExtendedStringBuilder sb, object o) => sb.Append(o);
public static implicit operator string(ExtendedStringBuilder sb) => sb.CurrentString;
public override string ToString() => CurrentString;
public string ToString(int startIndex, int length) => _stringBuilder.ToString(startIndex, length);
}
I didn't implement all the overloads of the .Append method (yet) or the + variants of them.
This can literally be used in the exact same manner as the .NET StringBuilder, or you can use += or + instead of .Append, and you can implicitly convert it to a string.
StringBuilderas an argument. \$\endgroup\$