title: C# - Return generic array type
body: <p>If I have a simple Utility function that copies an array to a new array:</p>
  
  <pre><code>public static object[] CopyTo(object[] original, int startIndex, int endIndex)
  {
      List&lt;object&gt; copied - new List&lt;object&gt;();
      for (int i = startIndex; i &lt;= endIndex; i++) 
      {
          copied.Add(original[i]);
      }
      return copied.ToArray();
  }
  </code></pre>
  
  <p>and I want to then be able to call it like this:</p>
  
  <pre><code>int[] newThing = CopyTo(new int[] { 10, 9, 8, 7, 6 }, 2, 4);
  </code></pre>
  
  <p>the compiler errors saying <code>cannot convert from int[] to object[]</code>.  This is expected since my <code>CopyTo</code> function specifically wants an object array, not an integer array.</p>
  
  <p><strong>How can I change the declaration of CopyTo in order for it to dynamically accept and return an array of any type?</strong>  I believe Generics is the way (though I'm not too familiar with this) so I tried:</p>
  
  <pre><code>public static T[] CopyTo(T[] original, int startIndex......)
  </code></pre>
  
  <p>but the compiler won't recognise T as a type.</p>