gpt4 book ai didi

c# - 为什么不必返回此数组?

转载 作者:行者123 更新时间:2023-11-30 22:06:43 25 4
gpt4 key购买 nike

我指的是本网站上的代码:http://www.dotnetperls.com/fisher-yates-shuffle

public static void Shuffle<T>(T[] array)
{
var random = _random;
for (int i = array.Length; i > 1; i--)
{
// Pick random element to swap.
int j = random.Next(i); // 0 <= j <= i-1
// Swap.
T tmp = array[j];
array[j] = array[i - 1];
array[i - 1] = tmp;
}
}

为什么Shuffle<T>(T[] array)中的数组不是必须要返回吗?我想为了改变一个变量的传入值,并让它影响调用它的方法中的变量,你必须使用 ref .那么为什么没有ref呢?正在使用?

最佳答案

来自 http://msdn.microsoft.com/en-us/magazine/cc301755.aspx

Arrays are mechanisms that allow you to treat several items as a single collection. The Microsoft® .NET Common Language Runtime (CLR) supports single-dimensional arrays, multidimensional arrays, and jagged arrays (arrays of arrays). All array types are implicitly derived from System.Array, which itself is derived from System.Object. This means that all arrays are always reference types which are allocated on the managed heap, and your app's variable contains a reference to the array and not the array itself.

因为它们是引用类型,所以所讨论的函数正在接收对数组的引用。

关于c# - 为什么不必返回此数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23438516/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com