Selecting a range of items inside an array in C#

C#.NetArrays

C# Problem Overview


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?

C# Solutions


Solution 1 - C#

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmouthpiecView Question on Stackoverflow
Solution 1 - C#Marc GravellView Answer on Stackoverflow