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");)