Tags: , , , | Categories: .NET, Interview questions Posted by oleksii on 1/1/2012 4:51 PM | Comments (0)

This is one of the typical interview questions: when do I need to use a StringBuilder and how a simple implementation would look like?

The problem with a System.String type is that it is actually an immutable reference type although it seems to behave like mutable value type. Immutability means that for any write operation performed with the same string, a new object is mandatory created. For example, s1 = s1 + s2 will not modify s1, but will create a new object with a concatenated s1 and s2 and drops the reference to the old s1 to garbage collector. This is required to support long strings (< 2GB in .NET 4), save time and space complexity and maybe (which would be a pure guess) it was easier to follow a successful example of Java strings.

StringBuilder class has internal access to the string object and is useful for any string manipulations, especially numerous. Internally, StringBuilder uses string object, and since .NET 4 it uses a char array. This class does not create copies of string objects but rather acts as if it works with one mutable string

Because StringBuilder implements a Builder design pattern any state-changing operation should return the object itself (this). ToString() method acts as a Build() method and as it is defined in the System.Object, there is no need to include ToString() in the contract.

public interface ISimpleStringBuilder
{
	ISimpleStringBuilder Append(string value);
	ISimpleStringBuilder Clear();
	int Lenght { get; }
	int Capacity { get; }
}

A very simple implementation of the builder class may look like this

public class SimpleStringBuilder : ISimpleStringBuilder
{
    private char[] _internalBuffer;

    public ISimpleStringBuilder Append(string value)
    {
        char[] data = value.ToCharArray();

        //check if space is available for additional data
        InternalEnsureCapacity(data.Length);

        foreach (char t in data)
        {
            _internalBuffer[Lenght] = t;
            Lenght++;
        }

        return this;
    }

    public override string ToString()
    {
        //use only non-null ('\0') characters
        var tmp = new char[Lenght];
        for (int i = 0; i < Lenght; i++)
        {
            tmp[i] = _internalBuffer[i];
        }
        return new string(tmp);
    }
    ...    
}

This code is of course very basic, inefficient, not thread-safe, doesn't make any input validation etc. It does however demonstrates the idea behind StringBuilder class.

SimpleStringBuilder.zip (75.77 kb) [Downloads: 143]

If you enjoyed this post, make sure you subscribe to my RSS feed!

blog comments powered by Disqus