在扩展方法中更改数组的大小不起作用

本文关键字:不起作用 数组 扩展 方法 | 更新日期: 2023-09-27 18:36:30

所以基本上我为数组类型编写了我的小Add扩展方法。

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

输出应该是Hello cruel but funny world的,但but funny永远不会在扩展方法中连接。

在扩展中编辑相同的数组似乎也不起作用:

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

我在这里做错了什么,我怎样才能将其用作扩展?

.dotNet 小提琴:https://dotnetfiddle.net/9os8nY 或 https://dotnetfiddle.net/oLfwRD

(找到一种方法让我可以保持通话test.Add("item");会很棒)

在扩展方法中更改数组的大小不起作用

您正在为参数分配一个新的引用,除非您将其作为参数传递ref否则它不会更改实际数组。由于这是一种扩展方法,因此不是一种选择。因此,请考虑使用常规方法:

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

或者,如果您坚持使用扩展方法,则需要使数组成为第二个参数才能使用 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不起作用。因为它改变了_self,而不是test数组。现在,当您传递不带ref关键字的引用类型时,将复制该引用。它是这样的:

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

现在,如果你给arr2分配一个新的引用,它不会改变arr1的引用。Array.Resize所做的是,由于无法调整数组的大小,它会创建一个新数组并将所有元素复制到新数组中,并将该新引用分配给参数(在这种情况下_self)。因此,它更改了_self指向的位置,但由于_selftest是两个不同的引用(如arr1arr2),更改其中一个不会影响另一个。

另一方面,如果您像我的第一个示例中一样将数组ref传递给您的方法,则Array.Resize也将按预期工作,因为在这种情况下,不会复制引用:

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

_self =会将_self对象的副本创建到为参数创建的局部变量中 _self - 因此原始对象不会更新。 使用引用类型(如列表),或创建返回新数组的静态方法。

就原因的"感觉"而言:您正在变量实例上调用一个方法 - 您无法从在其上下文中执行的代码中更改该实例

您可以像这样更改代码:

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

作为旁注 - 用法将与 Concat 方法相同。