Generic extension method for an array does not compile <p>Populating a request object for a web-service, I need to dynamically add items to some arrays. </p> <p>I hoped to simplify it by implementing an extension method:</p> <pre><code>public static class ArrayExtensions&lt;T&gt; where T : class { public static T[] Extend&lt;T&gt;(T[] originalArray, T addItem) { if (addItem == null) { throw new ArgumentNullException("addItem"); } var arr = new[] { addItem }; if (originalArray == null) { return arr; } return originalArray.Concat(arr).ToArray(); } } </code></pre> <p>So that this old code:</p> <pre><code>if (foo.bazArr == null) { foo.bazArr = new[] { baz }; } else { foo.bazArr = new[] { baz }.Concat(foo.bazArr).ToArray(); // (i know this inserts the new item at the beginning, but that's irrelevant, order doesn't matter) } </code></pre> <p>could be rewritten as:</p> <pre><code>foo.bazArr = foo.bazArr.Extend(baz); // won't compile </code></pre> <p>The error is: <code>'System.Array' does not contain a definition for 'Extend' and no extension method 'Extend' accepting a first argument of type 'System.Array' could be found (are you missing a using directive or an assembly reference?)</code></p> <p>Whereas calling the extension method directly like so:</p> <pre><code>foo.bazArr = ArrayExtensions&lt;someService.bazType&gt;.Extend(foo.bazArr, baz); </code></pre> <p>compiles fine.</p> <p>Why is that so? Why can't the compiler infer the type on its own here, if the array is strongly-typed?</p> <hr> <p><strong>EDIT</strong> - correct code below:</p> <pre><code>public static class ArrayExtensions { public static T[] Extend&lt;T&gt;(this T[] originalArray, T addItem) where T : class { if (addItem == null) { throw new ArgumentNullException("addItem"); } var arr = new[] { addItem }; if (originalArray == null) { return arr; } return originalArray.Concat(arr).ToArray(); // although Concat is not recommended for performance reasons, see the accepted answer } } </code></pre> <hr> <p>For this popular question, here's another good simple example:</p> <pre><code>public static class Extns { // here's an unbelievably useful array handling extension for games! public static T AnyOne&lt;T&gt;(this T[] ra) where T:class { int k = ra.Length; int r = Random.Range(0,k); return ra[r]; // (add your own check, alerts, etc, to this example code) } } </code></pre> <p>and in use ..</p> <pre><code>someArrayOfSoundEffects.AnyOne().Play(); someArrayOfAnimations.AnyOne().BlendLeft(); winningDisplay.text = successStringsArray.AnyOne() +", " +playerName; SpawnEnormousRobotAt( possibleSafeLocations.AnyOne() ); </code></pre> <p>and so on. For any array it will give you one random item. Used constantly in games to randomise effects etc. The array can be any type.</p>