.NET equivalent of Java List.subList ()?

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

+13
java collections list sublist
Aug 17 '09 at 11:01
source share
3 answers

using LINQ

 list.Skip(fromRange).Take(toRange - fromRange) 
+10
Aug 17 '09 at 11:18
source share

For a general List<T> this is the GetRange(int, int) method.

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

Edit2: as Camarey points out, you can have read-only access:

 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 text will print 7, 42, 9.

+9
Aug 17 '09 at 11:03
source share

GetRange is your answer

+1
Aug 17 '09 at 11:05
source share



All Articles