8
\$\begingroup\$

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.

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

A couple of quick comments:

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.

\$\endgroup\$
  • \$\begingroup\$ 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.) \$\endgroup\$ – Der Kommissar Apr 19 '16 at 7:47
  • \$\begingroup\$ @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. \$\endgroup\$ – RobH Apr 19 '16 at 7:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.