从列表中选择特定范围内的所有项目并将其放入新列表中的最有效方法是什么?
List<DataClass> xmlList = new List<DataClass>();
这是我的List,我想将所有DataClass项放在新List中的范围(3 - 7)之间.
什么是最有效的方式?一个foreach循环,每次计数++,直到他到达范围之间的项目并将这些项目添加到新列表?
Lig*_*ker 77
您正在寻找的方法是GetRange:
List<int> i = new List<int>();
List<int> sublist = i.GetRange(3, 4);
var filesToDelete = files.ToList().GetRange(2, files.Length - 2);
从摘要:
// Summary:
//     Creates a shallow copy of a range of elements in the source System.Collections.Generic.List<T>.
// Parameters:
//   index:
//     The zero-based System.Collections.Generic.List<T> index at which the range
//     starts.
//   count:
//     The number of elements in the range.
Cle*_*ens 28
如果由于任何原因您不想使用GetRange方法,您还可以使用LINQ编写以下内容.
List<int> list = ...
var subList = list.Skip(2).Take(5).ToList();
| 归档时间: | 
 | 
| 查看次数: | 69127 次 | 
| 最近记录: |