Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 317
ripper234 Avatar asked Aug 17 '09 11:08

ripper234


3 Answers

using LINQ

list.Skip(fromRange).Take(toRange - fromRange)
like image 73
Kamarey Avatar answered Nov 15 '22 16:11

Kamarey


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.

like image 22
Razzie Avatar answered Nov 15 '22 16:11

Razzie


GetRange is your answer

like image 1
dfa Avatar answered Nov 15 '22 17:11

dfa