Should I create an empty string and append through a foreach loop or should I use StringBuilder? Here's the current code: 

    static string GetResources(string header, string filter, string resourceTemplate)
    {
        string themeName;
        if (!TryGetHeaderValue(header, "theme", out themeName)) return "";

        var resources = "";
        var path = Path.Combine(HtmlPreview.BaseDirectory, themeName);

        foreach (var resource in Directory.GetFiles(path, filter))
        {
            resources += String.Format(
                resourceTemplate,
                themeName,
                Path.GetFileName(resource));
        }

        return resources;
    }


I was wondering if I should use string builder instead: 

static string GetResources(string header, string filter, string resourceTemplate)
    {
        string themeName;
        if (!TryGetHeaderValue(header, "theme", out themeName)) return "";

        var resourcesStringBuilder = new StringBuilder();
        var path = Path.Combine(HtmlPreview.BaseDirectory, themeName);

        foreach (var resource in Directory.GetFiles(path, filter))
        {
            resourcesStringBuilder.Append(String.Format(
                resourceTemplate,
                themeName,
                Path.GetFileName(resource)));
        }

        return resourcesStringBuilder.ToString();
    }


Resharper simply suggests it is possible to replace the original code with a LINQ statement:

    static string GetResources(string header, string filter, string resourceTemplate)
    {
        string themeName;
        if (!TryGetHeaderValue(header, "theme", out themeName)) return "";

        var path = Path.Combine(HtmlPreview.BaseDirectory, themeName);

        return Directory.GetFiles(path, filter)
            .Aggregate("", 
            (current, resource) =&gt; current + 
                String.Format(resourceTemplate, 
                themeName, 
                Path.GetFileName(resource)));
    }


Thank you! The original code with context is also on github.
Strings in C# are immutable which means that this:

 resouces += string.Format(...);


will create 2 strings: one from string.Format and then the new string created by appending it to the existing string.

A StringBuilder will just keep a list of all the strings to append and create one result string at the end when you call ToString(). So instead of 2*n strings you only create n+1 strings. The Aggregate has the same problem.

There are some optimizations in the C# compiler which can transform string + string + string + ... sequences into Append calls on a StringBuilder but you should not really rely on this.

In the end it doesn't matter too much if your strings a short. However when you hit the 85kb limit then the strings will be placed on the large object heap which can cause problems because it doesn't get compacted and you can get holes. This can lead to weird out of memory situations (you can find more with google).