# An extension to the StringBuilder

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.

• What I would add is a (copy)constructor which takes a StringBuilder as an argument. – Heslacher Apr 19 '16 at 7:48

You can use the see tag's cref attribute. If you generate documentation, some tools will generate hyperlinks for you.

/// <summary>
/// This wraps the .NET <see cref="StringBuilder"/> in a slightly easier to use format.
/// </summary>


The length property of a StringBuilder is read and writable. It's also really useful for it to be so:

var sb = new StringBuilder();
foreach (var i in Enumerable.Range(0,10))
{
sb.AppendFormat("{0},", i);
}
sb.Length--; // removes the trailing comma.


That's a contrived example which is trivially served with string.Join but setting the length can be useful!

The _stringBuilder field should be readonly.

I'd say CurrentString is superflous. Just call _stringBuilder.ToString()

I must admit, personally I think the StringBuilder api is really good, I've never needed an implicit conversion to a string or felt the need to + rather than append to them.

• Except that see generates a link to that location, which cannot be followed for a link to an internal .NET library reference. (Which is why I don't use it for them.) – Der Kommissar Apr 19 '16 at 7:47
• @EBrown - When I used to do documentation and used Sandcastle, I thought it was capable of generating MSDN links for framework classes? If you're not using Sandcastle, or I've misremembered, I'll remove that bit. – RobH Apr 19 '16 at 7:55