C# StringBuilder related question

Posted by Sef on Stack Overflow See other posts from Stack Overflow or by Sef
Published on 2010-04-11T16:24:17Z Indexed on 2010/04/11 16:33 UTC
Read the original article Hit count: 319

Filed under:

Hello,

I have written a program for a stack. For this i needed a StringBuilder to be able to show me what was in the stack else i would get the class name instead of the actual values inside.

My question is there any other way except for a StringBuilder for such kind of problem?
Also in what other kind of cases does this kind of problem happen? Also the way i have written the StringBuilder felt very awkward when i needed several things on 1 line.

    public override string ToString()
    {
        StringBuilder builder = new StringBuilder();
        foreach (int value in tabel)
        {
            builder.Append(value);
            builder.Append("   ");
        }
        if (tabel.Length == tabel.Length) // this is a bit messy, since I couldn't append after the rest above
        {
            builder.Append("(top:");
            builder.Append(top);
            builder.Append(")");
        }
        return builder.ToString();
    }/*ToString*/ 

Regards.

© Stack Overflow or respective owner

Related posts about c#