Search code examples
c#arraysunity-game-engineextension-methodsconcatenation

Changing size of array in extension method does not work?


So basically I wrote my little Add extension method for array types.

using System;
using System.Linq;
public static class Extensions 
{
    public static void Add<T>(this T[] _self, T item)
    {
        _self = _self.Concat(new T[] { item }).ToArray();
    }
}
public class Program
{
    public static void Main()
    {
        string[] test = { "Hello" };
        test = test.Concat(new string[] { "cruel" }).ToArray();
        test.Add("but funny");
        Console.WriteLine(String.Join(" ", test) + " world");
    }
}

The output should be Hello cruel but funny world, but the but funny will never be concatenated within the extension method.

Editing the same array in the extension seems not to work, too:

using System;
using System.Linq;
public static class Extensions 
{
    public static void Add<T>(this T[] _self, T item)
    {
        Array.Resize(ref _self, _self.Length + 1);
        _self[_self.Length - 1] = item;
    }
}
public class Program
{
    public static void Main()
    {
        string[] test = { "Hello" };
        test = test.Concat(new string[] { "cruel" }).ToArray();
        test.Add("but funny");
        Console.WriteLine(String.Join(" ", test) + " world");
    }
}

What did I get wrong here, how can I use it as extension?

.dotNet fiddle: https://dotnetfiddle.net/9os8nY or https://dotnetfiddle.net/oLfwRD

(It would be great to find a way so I can keep the call test.Add("item");)


Solution

  • You are assigning a new reference to the argument,it won't change the actual array unless you pass it as ref parameter. Since this is an extension method it's not an option. So consider using a normal method:

    public static void Add<T>(ref T[] _self, T item)
    {
        _self = _self.Concat(new T[] { item }).ToArray();
    }
    
    Add(ref test, "but funny");
    

    Or if you insist on using extension methods you need to make the array second parameter in order to use ref:

    public static void AddTo<T>(this T item, ref T[] arr, )
    {
        arr = arr.Concat(new T[] { item }).ToArray();
    }
    
    "but funny".AddTo(ref test);
    

    Array.Resize doesn't work. Because it changes the _self, not the test array. Now when you pass a reference type without ref keyword, the reference is copied. It's something like this:

    string[] arr1 = { "Hello" };
    string[] arr2 = arr1;
    

    Now, if you assign a new reference to arr2, it will not change arr1's reference.What Array.Resize is doing is that since resizing an array is not possible, it creates a new array and copies all elements to a new array and it assigns that new reference to the parameter (_self in this case).So it changes the where _self points to but since _self and test are two different references (like arr1 and arr2), changing one of them doesn't affect the other.

    On the other hand if you pass array as ref to your method as in my first example, the Array.Resize will also work as expected, because in this case the reference will not be copied:

    public static void Add<T>(ref T[] _self, T item)
    {
        Array.Resize(ref _self, _self.Length + 1);
        _self[_self.Length - 1] = item;
    }