Generic extension method for an array does not compile
Asked Answered
L

3

6

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.

Lesbian answered 14/5, 2012 at 6:50 Comment(1)
try adding a this before T[] originalArray in your method paramHeraclitus
D
6

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.

Detrition answered 14/5, 2012 at 6:53 Comment(5)
This is embarassing :)) gosh, I really need that first coffee today. Thank you.Lesbian
1. By the way, the compiler also forced me to move the generic constraint from the class level to the method level. But it works fine when I did that. 2. A List<T> would be far preferable. Check to see if your web-service tools offer lists as an option. - I imagine. But we've got other apps using this webservice and changing the type surely would break them here, no? I only expect a few items in the collection anyway (they represent product sizes, so it's never thousands of them).Lesbian
@Morawski the list vs array is a client-side thing; doesn't impact the web-service or the serialization at all.Detrition
Accepted as the first and most comprehensive answer.Lesbian
I agree that List<> is far preferable. However, I did throw together an extension method that concatenates 2 arrays by allocating a new one and copying both original arrays into it, as Marc described. dotnetfiddle.net/Igzu9CMornay
E
2

use this in defining extension method

public static T[] Extend<T>(this T[] originalArray, T addItem)
Estey answered 14/5, 2012 at 6:53 Comment(0)
B
1

you have missed the "this" keyword

Bathurst answered 14/5, 2012 at 6:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.