Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
473 views
in Technique[技术] by (71.8m points)

.net - Selecting a range of items inside an array in C#

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...