正确的形式在C#中切片数组

Har*_*uds 6 c# arrays

我想问一下如何在不使用Array.Copy的情况下切片数组.我会给你一个我想要实现的例子,这样你就能理解我.

假设我有这个阵列.叫原创

[1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15]

我想从一个起始索引得到一个复制数组给定一些长度,让我说我想要元素一到元素6我希望代码执行任务,如

int startIndex = 0;
int lenght= 5;
int[] CopyArray = ArrayFromRange(Original, startIndex, length);
Run Code Online (Sandbox Code Playgroud)

然后copyArray将是:

[1 | 2 | 3 | 4 | 5]我不想使用Array.Copy,因为我将循环它以获得后续切片

所以我会这样做

int length = 3;
for(int i = 0; i < OriginalArray.Length; i++)
{
     int[] CopyArray = ArrayFromRange(OriginalArray, i, length);
     // perform some operations
}
Run Code Online (Sandbox Code Playgroud)

每次循环执行它时,这将给我一个包含4个元素的数组,然后我会做一些操作.但是如果我这样Array.Copy做会抛出一个OutOfBoundsException当循环中的i得到值13时它会尝试复制不存在的数组[15].我想避免这些错误.

我正在开发Winforms,.Net 4.0

Kat*_*314 8

我认为处理您的方法的最佳方法是使用IEnumerable对象,特别是使用LINQ.IEnumerable是一个界面,意思是"你可以在这个对象上调用foreach".它是为数组实现的,也是用于查询集合的其他一些对象的实现 - 部分原因是,你并不特别需要知道那些对象是什么; 只需在需要时枚举每个项目.

using System.Linq; // put this at the top of the .cs, not mid-code.

int startIndex = 0;
int lenght= 5;
IEnumerable<int> CopyArray = Original.Skip(startIndex).Take(lenght);
Run Code Online (Sandbox Code Playgroud)

我在记事本中写道,所以我可能会错过一些小事,但这应该可以满足您的需求.如果它始终为0,则可以跳过.Skip(startIndex)部分.要访问每个int:

foreach (int value in CopyArray) {
   // TODO with value??
}
Run Code Online (Sandbox Code Playgroud)