title: Ways to improve string memory allocation
body: <p>This question is more theoretical than practical, but still.</p>
  
  <p>I've been looking for a chance to improve the following code from the string memory allocation standpoint:</p>
  
  <pre><code>/* Output for n = 3:
  *
  *  '  #'
  *  ' ##'
  *  '###'
  *
  */
  public static string[] staircase(int n) {
      string[] result = new string[n];
  
      for(var i = 0; i &lt; result.Length; i++) {
          var spaces = string.Empty.PadLeft(n - i - 1, ' ');
          var sharpes = string.Empty.PadRight(i + 1, '#');
  
          result[i] = spaces + sharpes;
      }
  
      return result;
  }
  </code></pre>
  
  <p><a href="https://github.com/fixdpt/shared-source-cli-2.0/blob/d63349c09c2e93e4bfc4c8b147ff0805f36cec68/clr/src/vm/comstring.cpp#L1305-L1325" rel="nofollow noreferrer">PadHelper</a> is the method, that is eventually called under the hood twice per iteration.</p>
  
  <p>So, correct me if I'm wrong, but it seems like memory is allocated at least 3 times per iteration.</p>
  
  <p>Any code improvements will be highly appreciated.</p>