Populating a request object for a web-service, I need to dynamically add items to some arrays.
I hoped to simplify it by implementing an extension method:
public static class ArrayExtensions<T> where T : class
{
public static T[] Extend<T>(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();
}
}
So that this old 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)
}
could be rewritten as:
foo.bazArr = foo.bazArr.Extend(baz); // won't compile
The error is: '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?)
Whereas calling the extension method directly like so:
foo.bazArr = ArrayExtensions<someService.bazType>.Extend(foo.bazArr, baz);
compiles fine.
Why is that so? Why can't the compiler infer the type on its own here, if the array is strongly-typed?
EDIT - correct code below:
public static class ArrayExtensions
{
public static T[] Extend<T>(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
}
}
For this popular question, here's another good simple example:
public static class Extns
{
// here's an unbelievably useful array handling extension for games!
public static T AnyOne<T>(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)
}
}
and in use ..
someArrayOfSoundEffects.AnyOne().Play();
someArrayOfAnimations.AnyOne().BlendLeft();
winningDisplay.text = successStringsArray.AnyOne() +", " +playerName;
SpawnEnormousRobotAt( possibleSafeLocations.AnyOne() );
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.
this
beforeT[] originalArray
in your method param – Heraclitus