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

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.

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

var myNewList  = myList.Skip(50).Take(10);

http://msdn.microsoft.com/en-us/library/bb503062.aspx

http://msdn.microsoft.com/en-us/library/bb357513.aspx

myList.Skip(50).Take(10)

What about:

        List<string> myList = new List<string>();

        //added 100 of Items to myList
        for (int i=0; i<100; ++i)
        {
            myList.Add("blablabla");
        }

        //then I want to grab items at indices 50 - 60
        // Note: 50 - 60 inclusivly are actually 11 items
        List<string> myNewList = myList.GetRange(50, 11); 

Need Your Help

Alphabetizing methods in Visual Studio

visual-studio visual-studio-2008 coding-style resharper organization

Is there any sort of plug-in or tool available for Visual Studio 2008 to alphabetize methods? Ideally I'd like a tool that will alphabetize a selection, or specified type (i.e. only methods, not m...

Double pointer const-correctness warnings in C

c const implicit-conversion qualifiers multiple-indirection

A pointer to non-const data can be implicitly converted to a pointer to const data of the same type: