简体   繁体   English

.NET相当于Java的List.subList()?

[英].NET equivalent of Java's List.subList()?

是否有一个.NET的等效Java的List.subList()适用于IList<T>

使用LINQ

list.Skip(fromRange).Take(toRange - fromRange)

For the generic List<T> , it is the GetRange(int, int) method. 对于通用List<T> ,它是GetRange(int, int)方法。

Edit: note that this is a shallow copy, not a 'view' on the original. 编辑:请注意,这是浅拷贝,而不是原始的“视图”。 I don't think C# offers that exact functionality. 我不认为C#提供了确切的功能。

Edit2: as Kamarey points out, you can have a read-only view: Edit2:正如Kamarey所指出的,你可以有一个只读视图:

List<int> integers = new List<int>() { 5, 6, 7, 8, 9, 10, 11, 12 };
IEnumerable<int> view = integers.Skip(2).Take(3);
integers[3] = 42;

foreach (int i in view )
  // output

The above will print 7, 42, 9. 以上将打印7,42,9。

GetRange是你的答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM