无效数组扩展方法什么都不做
Void array extension method does nothing
我写了一个数组,然后使用我的扩展方法附加到它:
private static void Main(string[] args)
{
int[] testInts = { 1, 2, 3, 4, 5 };
testInts.Append(6);
Console.WriteLine("Test Integers\n");
foreach (int variable in testInts)
{
Console.WriteLine(Convert.ToString(variable));
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void Append(this int[] arr, int value)
{
arr = arr.Concat(new[] { value }).ToArray();
}
但是当它打印到控制台时,它仍然只有 1, 2, 3, 4, 5?但是当我只使用
testInts = testInts.Concact(new[] { 6 }).ToArray()
效果还好吗?为什么会这样,有什么办法可以解决这个问题吗?
编辑
我使用 void 的全部意义在于我不想使用
testInt = testInt.Append(6)
它必须只是 testInt.Append(6)
。有什么办法吗?
为什么会这样
当您将 testInts 数组传递给扩展方法时,它并没有真正传递变量,而只是传递引用。因此,当您进行赋值时,它确实会创建具有第 6 个值的本地数组并将其分配给局部变量(而不是原始调用者的变量)。
您需要通过 ref 传递变量,但这对于扩展方法是不可能的。
要更好地理解这一点,请检查 passing parameters by reference,带或不带 ref 关键字。
Because the parameter is a reference to arr, it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable, arr.
使用扩展方法使其工作
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] testInts = { 1, 2, 3, 4, 5 };
testInts.Append(ref testInts,6);
Console.WriteLine("Test Integers\n");
foreach (int variable in testInts)
{
Console.WriteLine(Convert.ToString(variable));
}
Console.WriteLine("\nPress any key to continue.");
}
}
public static class Extentions {
public static void Append(this int[] arr, ref int[] arr2, int value)
{
arr2 = arr.Concat(new[] { value }).ToArray();
}
}
另一种让它起作用的方法
如果要进行新的赋值,即使进行扩展方法也没有意义。
public static int[] Append(int[] arr, int value)
{
return arr.Concat(new[] { value }).ToArray();
}
// use like this
arr = Append(arr, 6);
如果您调试应用程序,当您使用 Append(.., ..) 方法时,您会观察到 "arr" 在 Concat() 操作后具有更新值 (1..6),但是一旦控制从 Append(.., ..) 方法中出来,您的 "testInts" 将具有旧值 (1..5).
尝试更新代码。
public static int[] Append(this int[] arr, int value)
{
arr = arr.Concat(new[] { value }).ToArray();
return arr;
}
并在主要(..)
testInts = testInts.Append(6);
这样你会得到预期的结果。
那是因为数组是一个reference type and NOT a value type。
您需要 "replace" 数组与新数组以获得所需的结果。
这就像你想要的那样工作:
testInts = testInts.Concat(new[] { 6 }).ToArray()
因为你在传递数组(引用类型对象)时再次设置了数组,你传递的是指向数组(在应用程序内存中)的指针,这就是为什么你需要更换它。如果您想更好地理解这个主题,请阅读 Pointers。
这里是你的代码的固定版本,看到扩展方法现在返回一个新的整数数组。
private static void Main(string[] args)
{
int[] testInts = { 1, 2, 3, 4, 5 };
testInts = testInts.Append(6);
Console.WriteLine("Test Integers\n");
foreach (int variable in testInts)
{
Console.WriteLine(Convert.ToString(variable));
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static int[] Append(this int[] arr, int value)
{
arr = arr.Concat(new[] { value }).ToArray();
return arr;
}
我写了一个数组,然后使用我的扩展方法附加到它:
private static void Main(string[] args)
{
int[] testInts = { 1, 2, 3, 4, 5 };
testInts.Append(6);
Console.WriteLine("Test Integers\n");
foreach (int variable in testInts)
{
Console.WriteLine(Convert.ToString(variable));
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static void Append(this int[] arr, int value)
{
arr = arr.Concat(new[] { value }).ToArray();
}
但是当它打印到控制台时,它仍然只有 1, 2, 3, 4, 5?但是当我只使用
testInts = testInts.Concact(new[] { 6 }).ToArray()
效果还好吗?为什么会这样,有什么办法可以解决这个问题吗?
编辑
我使用 void 的全部意义在于我不想使用
testInt = testInt.Append(6)
它必须只是 testInt.Append(6)
。有什么办法吗?
为什么会这样
当您将 testInts 数组传递给扩展方法时,它并没有真正传递变量,而只是传递引用。因此,当您进行赋值时,它确实会创建具有第 6 个值的本地数组并将其分配给局部变量(而不是原始调用者的变量)。
您需要通过 ref 传递变量,但这对于扩展方法是不可能的。
要更好地理解这一点,请检查 passing parameters by reference,带或不带 ref 关键字。
Because the parameter is a reference to arr, it is possible to change the values of the array elements. However, the attempt to reassign the parameter to a different memory location only works inside the method and does not affect the original variable, arr.
使用扩展方法使其工作
using System;
using System.Linq;
public class Program
{
public static void Main(string[] args)
{
int[] testInts = { 1, 2, 3, 4, 5 };
testInts.Append(ref testInts,6);
Console.WriteLine("Test Integers\n");
foreach (int variable in testInts)
{
Console.WriteLine(Convert.ToString(variable));
}
Console.WriteLine("\nPress any key to continue.");
}
}
public static class Extentions {
public static void Append(this int[] arr, ref int[] arr2, int value)
{
arr2 = arr.Concat(new[] { value }).ToArray();
}
}
另一种让它起作用的方法
如果要进行新的赋值,即使进行扩展方法也没有意义。
public static int[] Append(int[] arr, int value)
{
return arr.Concat(new[] { value }).ToArray();
}
// use like this
arr = Append(arr, 6);
如果您调试应用程序,当您使用 Append(.., ..) 方法时,您会观察到 "arr" 在 Concat() 操作后具有更新值 (1..6),但是一旦控制从 Append(.., ..) 方法中出来,您的 "testInts" 将具有旧值 (1..5).
尝试更新代码。
public static int[] Append(this int[] arr, int value)
{
arr = arr.Concat(new[] { value }).ToArray();
return arr;
}
并在主要(..)
testInts = testInts.Append(6);
这样你会得到预期的结果。
那是因为数组是一个reference type and NOT a value type。
您需要 "replace" 数组与新数组以获得所需的结果。
这就像你想要的那样工作:
testInts = testInts.Concat(new[] { 6 }).ToArray()
因为你在传递数组(引用类型对象)时再次设置了数组,你传递的是指向数组(在应用程序内存中)的指针,这就是为什么你需要更换它。如果您想更好地理解这个主题,请阅读 Pointers。
这里是你的代码的固定版本,看到扩展方法现在返回一个新的整数数组。
private static void Main(string[] args)
{
int[] testInts = { 1, 2, 3, 4, 5 };
testInts = testInts.Append(6);
Console.WriteLine("Test Integers\n");
foreach (int variable in testInts)
{
Console.WriteLine(Convert.ToString(variable));
}
Console.WriteLine("\nPress any key to continue.");
Console.ReadKey();
}
public static int[] Append(this int[] arr, int value)
{
arr = arr.Concat(new[] { value }).ToArray();
return arr;
}