获取数组开始偏移 [英] Get array starting with offset

查看:91
本文介绍了获取数组开始偏移的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用C#,它是相当恼人的,我不能用C发送从某点开始的数组一样++。

I am using C#, and it's rather annoying that I can't send an array starting from a certain point like in C++.

想这code:

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

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

this is a weird syntax for C# because we don't have any usable pointers, but surely there's a way to do it? There's .Skip(), but I think it produces a new array, which is something I do not like.

我有哪些选择?

推荐答案

您可能希望把它作为一个的IEnumerable< INT> ,而不是作为一个数组。然后,您可以用跳跃,它会直接移动迭代器的元素个数跳过。用这种方式,你就不必使用ToArray的(),并创建有问题的阵列部分的副本。当然,IEnumerable的可能不适合你想做的事,但这是很难从你的问题告诉。

You might want to pass it as an IEnumerable<int> rather than as an array. You can then use skip and it will simply move the iterator over the number of elements skipped. Used this way, you won't have to use ToArray() and create a copy of the portion of the array in question. Of course, IEnumerable may not be appropriate for what you want to do, but that's difficult to tell from your question.

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

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

这篇关于获取数组开始偏移的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆