gpt4 book ai didi

c# - 获取以偏移量开头的数组

转载 作者:可可西里 更新时间:2023-11-01 08:55:32 25 4
gpt4 key购买 nike

我正在使用 C#,我不能像在 C++ 中那样从某个点开始发送数组,这很烦人。

假设这段代码:

int[] array = new int[32];
foobar (array + 4); //send array starting from the 4th place.

对于 C# 来说,这是一种奇怪的语法,因为我们没有任何可用的指针,但肯定有办法做到这一点吗?有 .Skip(),但我认为它会生成一个新数组,这是我不喜欢的。

我有哪些选择?

最佳答案

您可能希望将其作为 IEnumerable<int> 传递而不是作为一个数组。然后您可以使用 skip ,它只会将迭代器移动到跳过的元素数上。通过这种方式,您不必使用 ToArray() 并创建相关数组部分的副本。当然,IEnumerable 可能不适合您想要做的事情,但很难从您的问题中判断出来。

public void FooBar( IEnumerable<int> bar )
{
...
}

int[] array = new int[32];
FooBar( array.Skip(4) );

关于c# - 获取以偏移量开头的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1055923/

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