子使用 Ref 时需要关键字 Ref 吗?
Keyword Ref required at sub-use of Ref?
我在这个question
找到了这个方法
public static void RemoveAt<T>(ref T[] arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
arr[a] = arr[a + 1];
}
Array.Resize(ref arr, arr.Length - 1);
}
现在我想知道如果在嵌套方法中使用它是否需要 ref
?方法也可能如此:
public static void RemoveAt<T>(T[] arr, int index) //ref removed
具有相同的功能?我已经对其进行了测试并且它有效 - 但这意味着您可以在不传递 Ref
关键字的情况下更改引用。您可以在子方法中完成。
but that means you could change reference without passing Ref Keyword. You just could do it in a sub method
事实并非如此。尽管您可以在 中更改 您的 RemoveAt
方法的引用,但该更改不会影响传递给它的引用。您只需丢弃新的(调整大小的)实例即可。当您想更改 reference 以指向其他实例时,您的方法应该具有 ref
-关键字。
在其他关键字中你的第二个代码也可以这样写:
public static void RemoveAt<T>(arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
arr[a] = arr[a + 1];
}
var reference = arr;
Array.Resize(ref reference, arr.Length - 1);
}
虽然 reference
在调用 Array.Resize
后当然会发生变化,但 arr
将保持不变。他们引用了完全不同的实例。
功能将不一样。 Resize
可以更改对 arr
的引用,因此在第一种情况下,您将更改调用者的外部引用,而如果没有 ref
,您将只会更改本地方法引用.
参考:
var extArr = new object[100];
RemoveAt(ref extArr, 10); // The arr variable in this method is now the exact same physical
// variable as extArr.
// extArr is now a completely valid reference to the resized array, business as usual.
没有:
var extArr = new object[100];
RemoveAt(extArr , 10); // The copy of the reference (arr) is updated in this method
//internally, but the local variable extArr is only copied and not modified
// extArr is now a reference to the old, not-resized array.
// Note that the element is still "removed", overwritten in the for loop,
// but the resized copy of the array is lost and has no references to it.
我在这个question
找到了这个方法public static void RemoveAt<T>(ref T[] arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
arr[a] = arr[a + 1];
}
Array.Resize(ref arr, arr.Length - 1);
}
现在我想知道如果在嵌套方法中使用它是否需要 ref
?方法也可能如此:
public static void RemoveAt<T>(T[] arr, int index) //ref removed
具有相同的功能?我已经对其进行了测试并且它有效 - 但这意味着您可以在不传递 Ref
关键字的情况下更改引用。您可以在子方法中完成。
but that means you could change reference without passing Ref Keyword. You just could do it in a sub method
事实并非如此。尽管您可以在 中更改 您的 RemoveAt
方法的引用,但该更改不会影响传递给它的引用。您只需丢弃新的(调整大小的)实例即可。当您想更改 reference 以指向其他实例时,您的方法应该具有 ref
-关键字。
在其他关键字中你的第二个代码也可以这样写:
public static void RemoveAt<T>(arr, int index)
{
for (int a = index; a < arr.Length - 1; a++)
{
arr[a] = arr[a + 1];
}
var reference = arr;
Array.Resize(ref reference, arr.Length - 1);
}
虽然 reference
在调用 Array.Resize
后当然会发生变化,但 arr
将保持不变。他们引用了完全不同的实例。
功能将不一样。 Resize
可以更改对 arr
的引用,因此在第一种情况下,您将更改调用者的外部引用,而如果没有 ref
,您将只会更改本地方法引用.
参考:
var extArr = new object[100];
RemoveAt(ref extArr, 10); // The arr variable in this method is now the exact same physical
// variable as extArr.
// extArr is now a completely valid reference to the resized array, business as usual.
没有:
var extArr = new object[100];
RemoveAt(extArr , 10); // The copy of the reference (arr) is updated in this method
//internally, but the local variable extArr is only copied and not modified
// extArr is now a reference to the old, not-resized array.
// Note that the element is still "removed", overwritten in the for loop,
// but the resized copy of the array is lost and has no references to it.