开发者

Selecting a range of items inside an array in C#

开发者 https://www.devze.com 2023-01-05 02:53 出处:网络
I would like to select a range of items in an array of items. For example I have an array of 1000 items, and i would like to \"extract\" items 100 to 200 and开发者_开发百科 put them in another array.

I would like to select a range of items in an array of items. For example I have an array of 1000 items, and i would like to "extract" items 100 to 200 and开发者_开发百科 put them in another array.

Can you help me how this can be done?


In C# 8, range operators allow:

var dest = source[100..200];

(and a range of other options for open-ended, counted from the end, etc)

Before that, LINQ allows:

var dest = source.Skip(100).Take(100).ToArray();

or manually:

var dest = new MyType[100];
Array.Copy(source, 100, dest, 0, 100);
       // source,source-index,dest,dest-index,count
0

精彩评论

暂无评论...
验证码 换一张
取 消