Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Grabbing a part of the List<Item> by start and end indices

Tags:

c#

list

Is this possible?

For example, if I have

List<Item> myList = new List<Item>;

//added 100 of Items to myList

//then I want to grab items at indices 50 - 60

List<Item> myNewList = myList.?

How do I do that w/o looping through myList? Thank you.

like image 293
sarsnake Avatar asked Jul 19 '12 18:07

sarsnake


People also ask

Do lists have indexes C#?

The IndexOf method returns the first index of an item if found in the List. C# List<T> class provides methods and properties to create a list of objects (classes). The IndexOf method returns the first index of an item if found in the List.

Do lists start at 0 or 1 C#?

They are 0 based. Count will start with one however if the list has any items in it.


1 Answers

There is a method that retrieves these items, List.GetRange. http://msdn.microsoft.com/en-us/library/21k0e39c.aspx

List<Item> myList = new List<Item>;
myList.GetRange(50, 10); // Retrieves 10 items starting with index #50
like image 98
Ted Spence Avatar answered Oct 22 '22 02:10

Ted Spence