14

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

Jonik
  • 80,077
  • 70
  • 264
  • 372
ripper234
  • 222,824
  • 274
  • 634
  • 905

3 Answers3

10

using LINQ

list.Skip(fromRange).Take(toRange - fromRange)
Kamarey
  • 10,832
  • 7
  • 57
  • 70
  • 2
    Does this return a view into the original List or a copy of the range? – Joachim Sauer Aug 17 '09 at 11:23
  • Works on IList, not just on List, this is what I was looking for. – ripper234 Aug 17 '09 at 11:44
  • 1
    @Joachim Sauer: This is a query and it's result depends on how it's evaluated. While evaluating, it loops through list elements applying some rule. This isn't straight analog for Java's subList function, and it's mistake to compare them in this case. – Kamarey Aug 17 '09 at 12:12
  • 3
    @Kamarey: fair enough, but then the solution should have a disclaimer to that effect. Generally it seems that there are ways to achieve what you want, but there's no single 1:1 match for subList() (which is not a problem, but it should be noted). – Joachim Sauer Aug 17 '09 at 13:11
9

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.

Razzie
  • 30,834
  • 11
  • 63
  • 78
  • 2
    From the MSDN documentation (http://msdn.microsoft.com/en-us/library/21k0e39c.aspx) it doesn't seem to be the same: GetRange() returns a shallow copy, while subList() returns a view (i.e. changes to the subList() will be reflected in the original List!) – Joachim Sauer Aug 17 '09 at 11:05
  • ok you're right. Not sure if this is the exact requirement, but I'll edit my answer to reflect that. – Razzie Aug 17 '09 at 11:07
1

GetRange is your answer

Chuck Norris
  • 15,207
  • 15
  • 92
  • 123
dfa
  • 114,442
  • 31
  • 189
  • 228
  • 3
    Again: it's not *exactly* the same thing, as it returns a shallow copy instead of a view. Returning a view enables some pretty nifty tricks (it makes it unnecessary to provide a RemoveRange() method for example, since it can easily be written as list.subList(x,y).clear()). – Joachim Sauer Aug 17 '09 at 11:12
  • I must honestly say that I don't really see the benefits. Also, removeRange() and subList.clear() doesn't sound like the same thing, as clearing should not be the same as removing a range. But oh well :-) – Razzie Aug 17 '09 at 11:14
  • 5
    As soon as you grok the concept that subList() returns a view into the original List (and not a copy), it will become obvious that someList.subList(1,3).clear() will remove objects from the original List. And the most important part is not if it's better or not, but that subList() and GetRange() don't do the same thing, so they shouldn't be presented as equivalent (at least not without a disclaimer). – Joachim Sauer Aug 17 '09 at 11:21
  • Is there an equivalent extension method for IList ? – ripper234 Aug 17 '09 at 11:37
  • ah of course, clear() removes contents as well. I thought it set its content to null or a default value or something. – Razzie Aug 17 '09 at 11:48
  • @ripper234: there's a similar extension method, though it returns a read-only view. Not sure if that works for you, but see my updated answer. – Razzie Aug 17 '09 at 11:51