3

Does a StringBuilder variable need to be initialized to lets say string.empty in case you don't end up appending anything in the end and need to return something?

    public static string ToSemiColonList<T>(this IEnumerable<T> list, Func<T, string> func)
    {
        StringBuilder sb = new StringBuilder();

        foreach (T item in list)
        {
            if (sb.Length > 0)
                sb.Append(";");

            string elem = func(item);
            sb.Append(elem);
        }

        return sb.ToString();
    }

In this case it may never enter the foreach. So my guess is since every local variable in a method scope is set to no value in C#, to set it to an empty string since we need to give something back if this foreach is not hit.

Maybe it's better to check the stringbuilder length then if zero, return string.empty or am I going overboard (doing the same work twice) and it's fine like I have it?

PositiveGuy
  • 46,620
  • 110
  • 305
  • 471
  • 5
    No offense, but you have the code right there...it would have been faster to just test it than writing the question. – Randy Levy Jun 29 '10 at 04:13

1 Answers1

16

Default initialization of a StringBuilder object gets set to string.Empty.

The default capacity is 16 - see here for a similar (but different) question on SO.

Community
  • 1
  • 1
RPM1984
  • 72,246
  • 58
  • 225
  • 350
  • I guess I should be using Reflector?? how did you see what the default returned is for a StringBuilder? Really for any .NET native type? – PositiveGuy Jun 29 '10 at 04:22
  • How did you see the return type...? – PositiveGuy Jun 29 '10 at 04:29
  • No need for Relector...just RTM http://msdn.microsoft.com/en-us/library/dsd3ey60.aspx or debug/test your code to see the behavior. – Randy Levy Jun 29 '10 at 04:34
  • I looked at MSDN but it did not talk about any default value for SB unless I just missed it. – PositiveGuy Jun 29 '10 at 04:43
  • I think it's much faster though to use Reflector or MSDN...test and debug takes longer. – PositiveGuy Jun 29 '10 at 04:43
  • Thing is, you looked at the constructor page for StringBuilder. Unless I have time to sift through the entire MSDN to get the "right" page, I would have taken forever to find this "The string value of this instance is set to String.Empty," Because I was looking at this page initially and it mentions nowhere about the default initialization of the returned string http://msdn.microsoft.com/en-us/library/2839d5h5%28VS.71%29.aspx. I'm just trying to get more efficient with figuring out return values rather than debug all the time. Know what it does before you use it but MSDN sucks. – PositiveGuy Jun 29 '10 at 04:44
  • Also might I add you may not be able to debug if you've just changed a shitload of code and basically are trying to stub out initial logic. I just want to know what you typically do, sounds like you debug every time to figure out what the objects do. I don't know, to me it's not always possible to run your code, go create test code, etc. Point is, it's always a struggle for me in terms of the time issue so I'm thinking Reflector is probably the best way to go initially and look at the code behind the types. – PositiveGuy Jun 29 '10 at 04:47
  • 1
    @coffeeaddict: it depends on the scenario but for something fairly simple I would do a quick test. No need to debug the entire app, just take the code your interested in and create a small test app or use .NET Snippet Compiler ( http://geekswithblogs.net/MarkPearl/archive/2010/02/20/.net-snippet-compiler.aspx ). If you do the test yourself you will **definitely** know the behavior and you won't be mislead by bad documentation. I also find that I learn better that way. Plus I get the satisfaction of figuring something out. :) Of course, your mileage may vary. – Randy Levy Jun 29 '10 at 05:13