title: Iterating over IEnumerable, special casing last element
body: <p>I'm building a string based on an <code>IEnumerable</code>, and doing something like this:</p>
  
  <pre><code>public string BuildString()
  {
      var enumerable = GetEnumerableFromSomewhere(); // actually an in parameter,
                                                     // but this way you don't have to care
                                                     // about the type :)
  
      var interestingParts = enumerable.Select(v =&gt; v.TheInterestingStuff).ToArray();
  
      stringBuilder.Append("This is it: ");
  
      foreach(var part in interestingParts)
      {
          stringBuilder.AppendPart(part);
  
          if (part != interestingParts.Last())
          {
              stringBuilder.Append(", ");
          }
      }
  }
  
  private static void AppendPart(this StringBuilder stringBuilder, InterestingPart part)
  {
      stringBuilder.Append("[");
      stringBuilder.Append(part.Something");
      stringBuilder.Append("]");
  
      if (someCondition(part)) 
      {
           // this is in reality done in another extension method,
           // similar to the else clause
           stringBuilder.Append(" = @");
           stringBuilder.Append(part.SomethingElse");
      }
      else
      {
           // this is also an extension method, similar to this one
           // it casts the part to an IEnumerable, and iterates over
           // it in much the same way as the outer method.
           stringBuilder.AppendInFilter(part);
      }
  }
  </code></pre>
  
  <p>I'm not entirely happy with this idiom, but I'm struggling to formulate something more succinct.</p>
  
  <p>This is, of course, part of a larger string building operation (where there are several blocks similar to this one, as well as other stuff in between) - otherwise I'd probably drop the <code>StringBuilder</code> and use <code>string.Join(", ", ...)</code> directly.</p>
  
  <p>My closest attempt at simplifying the above, though, is constructs like this for each iterator:</p>
  
  <pre><code>stringBuilder.Append(string.Join(", ", propertyNames.Select(prop =&gt; "[" + prop + "]")));
  </code></pre>
  
  <p>but here I'm still concatenating strings with <code>+</code>, which makes it feel like the <code>StringBuilder</code> doesn't really contribute much.</p>
  
  <p>How could I simplify this code, while keeping it efficient?</p>