title: C# Add extension Append method to class array of type T
body: <p>Using this <a href="https://stackoverflow.com/a/1440325/5825579">answer</a> to the question "<em><a href="https://stackoverflow.com/questions/1440265/how-to-add-a-string-to-a-string-array-theres-no-add-function">How to add a string to a string[] array? There's no .Add function</a></em>" I am trying to use <a href="https://stackoverflow.com/a/1183291/5825579">this answer</a> to write an generic extension to append elements to an <a href="https://msdn.microsoft.com/en-us/library/ms228502.aspx" rel="nofollow noreferrer"><code>generic array</code></a>. Only using the <a href="https://msdn.microsoft.com/en-us/library/bb348051(v=vs.110).aspx" rel="nofollow noreferrer"><code>Array.Resize()</code></a> method works well and the sample below adds an extra element to my <a href="https://msdn.microsoft.com/en-us/library/0a7fscd0.aspx" rel="nofollow noreferrer"><code>string array</code></a></p>
  
  <pre><code>string[] array = new string[] { "Foo", "Bar" };
  Array.Resize(ref array, array.Length + 1);
  array[array.Length - 1] = "Baz";
  </code></pre>
  
  <p>But when I try to use the ArrayExtension method described below, the method <em>does</em> resize my array inside the method but when it returns the array is unchanged?</p>
  
  <p>My extension class</p>
  
  <pre><code>public static class ArrayExtensions
  {
      public static void Append&lt;T&gt;(this T[] array, T append)
      {
          Array.Resize(ref array, array.Length + 1);
          array[array.Length - 1] = append;    // &lt; Adds an extra element to my array
      }
  }
  </code></pre>
  
  <p>Used as follows</p>
  
  <pre><code>string[] array = new string[] { "Foo", "Bar" };
  array.Append("Baz");
  </code></pre>
  
  <p>When the method returns, the added element does not exist. What am I missing?</p>
  
  <p><strong>UPDATE</strong></p>
  
  <p>As pointed out, this question has been asked and answered here before. I will accept the previous question "<a href="https://stackoverflow.com/questions/27799341/changing-size-of-array-in-extension-method-does-not-work"><em>Changing size of array in extension method does not work?</em></a>" as an answer to my question.</p>
  
  <p><strong>UPDATE 2</strong></p>
  
  <p>Since if a return method is used in the extension method it does violate the expected behaviour of similar classes in the framework that Append() to modify the object itself, I did some changes to the extension method to prevent it from being falsely used. Thanks @NineBerry for pointing this out.</p>
  
  <p>I also added <a href="https://msdn.microsoft.com/sv-SE/library/w5zay9db.aspx" rel="nofollow noreferrer"><code>params T[] add</code></a> to allow for adding multiple elements at once.</p>
  
  <pre><code>public static class ArrayExtensions
  {
      /// &lt;summary&gt;
      /// Creates a copy of an object array and adds the extra elements to the created copy
      /// &lt;/summary&gt;
      /// &lt;typeparam name="T"&gt;&lt;/typeparam&gt;
      /// &lt;param name="array"&gt;&lt;/param&gt;
      /// &lt;param name="add"&gt;Elemet to add to the created copy&lt;/param&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static T[] CopyAddElements&lt;T&gt;(this T[] array, params T[] add)
      {
          for (int i = 0; i &lt; add.Length; i++)
          {
              Array.Resize(ref array, array.Length + 1);
              array[array.Length - 1] = add[i];
          }
          return array;
      }
  }
  </code></pre>
  
  <p>Usage</p>
  
  <pre><code>string[] array = new string[] { "Foo", "Bar" };
  array = array.CopyAddElements("Baz", "Foobar");
  for (int i = 0; i &lt; array.Length; i++)
  {
      System.Console.Write($"{array[i]} ");
  }
  
  /* Output
   *  Foo Bar Baz Foobar
  */
  </code></pre>