Question

If I am building a string using a StringBuilder object in a method, would it make sense to:

Return the StringBuilder object, and let the calling code call ToString()?

return sb;

OR Return the string by calling ToString() myself.

return sb.ToString();

I guess it make a difference if we're returning small, or large strings. What would be appropriate in each case? Thanks in advance.

Edit: I don't plan on further modifying the string in the calling code, but good point Colin Burnett.

Mainly, is it more efficient to return the StringBuilder object, or the string? Would a reference to the string get returned, or a copy?

Was it helpful?

Solution

Return the StringBuilder if you're going to further modify the string, otherwise return the string. This is an API question.

Regarding efficiency. Since this is a vague/general question without any specifics then I think mutable vs. immutable is more important than performance. Mutability is an API issue of letting your API return modifiable objects. String length is irrelevant to this.

That said. If you look at StringBuilder.ToString with Reflector:

public override string ToString()
{
    string stringValue = this.m_StringValue;
    if (this.m_currentThread != Thread.InternalGetCurrentThread())
    {
        return string.InternalCopy(stringValue);
    }
    if ((2 * stringValue.Length) < stringValue.ArrayLength)
    {
        return string.InternalCopy(stringValue);
    }
    stringValue.ClearPostNullChar();
    this.m_currentThread = IntPtr.Zero;
    return stringValue;
}

You can see it may make a copy but if you modify it with the StringBuilder then it will make a copy then (this is what I can tell the point of m_currentThread is because Append checks this and will copy it if it mismatches the current thread).

I guess the end of this is that if you do not modify the StringBuilder then you do not copy the string and length is irrelevant to efficiency (unless you hit that 2nd if).

UPDATE

System.String is a class which means it is a reference type (as opposed to value type) so "string foo;" is essentially a pointer. (When you pass a string into a method it passes the pointer, not a copy.) System.String is mutable inside mscorlib but immutable outside of it which is how StringBuilder can manipulate a string.

So when ToString() is called it returns its internal string object by reference. At this point you cannot modify it because your code is not in mscorlib. By setting the m_currentThread field to zero then any further operations on the StringBuilder will cause it to copy the string object so it can be modified and not modify the string object it returned in ToString(). Consider this:

StringBuilder sb = new StringBuilder();
sb.Append("Hello ");

string foo = sb.ToString();

sb.Append("World");

string bar = sb.ToString();

If StringBuilder did not make a copy then at the end foo would be "Hello World" because the StringBuilder modified it. But since it did make a copy then foo is still just "Hello " and bar is "Hello World".

Does that clarify the whole return/reference thing?

OTHER TIPS

I don't think performance should be a factor in this question. Either way someone is going to call sb.ToString() so your going to take the hit someplace.

The more important question is what is the intention of the method and the purpose. If this method is part of a builder you might return the string builder. Otherwise I would return a string.

If this is part of a public API I would lean towards returning a string instead of the builder.

I would say the method should return sb.ToString(). If the logic surrounding the creation of the StringBuilder() object should change in the future it makes sense to me that it be changed in the method not in each scenario which calls the method and then goes on to do something else

StringBuilder is an implementation detail of your method. You should return string until it becomes a performance problem, at which point you should explore another pattern (like visitor Pattern) that can help you introduce indirection and protect you from the internal implementation decisions.

Strings are always stored in the heap, so you will have a reference returned if the return type is string. You can't count, however, on two identical strings to have identical references. In general, it's safe to think of a string as if it were a value type even though it is actually a reference type.

I think it depends what you are doing with the string once it leaves the method. If you are going to continue appending to it then you might want to consider returning a stringbuilder for greater efficiency. If you are always going to call .ToString() on it then you should do that inside the method for better encapsulation.

I would return a string in almost all situations, especially if the method is part of a public API.

An exception would be if your method is just one part of a larger, private "builder" process and the calling code will be doing further manipulations. In that case then I'd maybe consider returning a StringBuilder.

Since your not going to modify it anymore

return sb.ToString();

should be most efficient

Return the sb.ToString(). Your method should concentrate on just the thing in hand (In this case build me a string) and not be returned to be further manipulated IMO, you could get into all sorts of problems with it not being disposed.

It depends on what you are planning on doing with the output. I would return a string personally. That way if you need to change the method down the road to not use a stringbuilder, you can as you won't be stuck with that as a return value.

Upon thinking about it for the moment, the answer is much clearer. The question of asking which should be returned really answers the question. The return object should be a string. The reason is that if you are asking the question, "Is there a reason to return the StringBuilder object when a string will do?" then the answer is no. If there was a reason, then returning string would be out of the question, because the methods and properties of the stringbuilder are needed.

If you need to append more stuff to the string and use other stringbuilder related functionality, return the stringbuilder. Otherwise if you're just using the string itself, return the string.

There are other more technical considerations, but thats the highest level concerns.

The method was given a concrete task and it should be expected to complete it and return the finished result that does not require further processing. Only return StringBuilder when you really need it. In that case also add something to the method name to indicate that you are returning something special.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top