tags:

views:

78

answers:

2

I know in python you can do something like myList[1:20] but is there anything similar to C#?

Thanks!

+11  A: 
var itemsOneThroughTwenty = myList.Take(20);
var itemsFiveThroughTwenty = myList.Skip(5).Take(15);
Tim Robinson
Note that these Linq extensions actually create an IEnumerable<> that stops after N items, rather than creating a new array/list. (This is kind of hidden by using 'var' for the variable type. If the code following the truncation iterates through the new list lots of times, you'll actually be re-evaluating the expression tree *each time*. In this case, you might want to tack a `.ToList()` onto the end to force the items to be enumerated and a new list created.
JaredReisinger
+6  A: 

You can use List.GetRange():

var subList = myList.GetRange(0, 20);
Dean Harding