gpt4 book ai didi

c# - 当源和目标是同一个数组时,Array.Copy 是否安全?

转载 作者:IT王子 更新时间:2023-10-29 04:14:44 25 4
gpt4 key购买 nike

我目前正在使用 Array.Copy 来移动数组:

Array.Copy(array, 0, array, 1, array.Length - 1);

它明显比使用循环更快。我知道其他语言(例如 memcpy)中的类似函数是未定义的,或者当编译器过于激进时有时会中断。认为这在 .NET 中是安全的是否合理?

最佳答案

是的,它是安全的。是documented该方法在这种情况下应该如何表现:

If sourceArray and destinationArray overlap, this method behaves as if the original values of sourceArray were preserved in a temporary location before destinationArray is overwritten.

关于c# - 当源和目标是同一个数组时,Array.Copy 是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11149668/

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