Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic extension method for an array does not compile

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.

like image 255
Konrad Morawski Avatar asked Jan 16 '23 16:01

Konrad Morawski


1 Answers

Missing this:

public static T[] Extend<T>(this T[] originalArray, T addItem)

Without the this it is not an extension method.

Additional note: extending an array one item at a time is expensive. A List<T> would be far preferable. Check to see if your web-service tools offer lists as an option.

Even with arrays, using Enumerable.Concat is probably overkill here; I would simply measure the two arrays, allocate a new one, and use the CopyTo method of each to write into place in the new array.

like image 77
Marc Gravell Avatar answered Jan 20 '23 16:01

Marc Gravell