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

别说谁变了你拦得住时间么 提交于 2019-12-19 05:13:59

问题


Is there a .NET equivalent of Java's List.subList() that works on IList<T>?


回答1:


using LINQ

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



回答2:


For the generic List<T>, it is the GetRange(int, int) method.

Edit: note that this is a shallow copy, not a 'view' on the original. I don't think C# offers that exact functionality.

Edit2: as Kamarey points out, you can have a read-only view:

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.




回答3:


GetRange is your answer



来源:https://stackoverflow.com/questions/1287340/net-equivalent-of-javas-list-sublist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!