在 C# 中选择数组中的一系列项目 [英] Selecting a range of items inside an array in C#
本文介绍了在 C# 中选择数组中的一系列项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在一组项目中选择一系列项目.例如,我有一个包含 1000 个项目的数组,我想提取"100 到 200 个项目并将它们放入另一个数组中.
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.
你能帮我怎么做吗?
推荐答案
在 C# 8 中,范围运算符允许:
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)
在此之前,LINQ 允许:
Before that, LINQ allows:
var dest = source.Skip(100).Take(100).ToArray();
或手动:
var dest = new MyType[100];
Array.Copy(source, 100, dest, 0, 100);
// source,source-index,dest,dest-index,count
这篇关于在 C# 中选择数组中的一系列项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文