How to Get a Sublist in C#

前端 未结 6 1560
走了就别回头了
走了就别回头了 2021-02-02 05:09

I have a List and i need to take a sublist out of this list. Is there any methods of List available for this in .NET 3.5?

6条回答
  •  猫巷女王i
    2021-02-02 05:16

    You want List::GetRange(firstIndex, count). See http://msdn.microsoft.com/en-us/library/21k0e39c.aspx

    // I have a List called list
    List sublist = list.GetRange(5, 5); // (gets elements 5,6,7,8,9)
    List anotherSublist = list.GetRange(0, 4); // gets elements 0,1,2,3)
    

    Is that what you're after?

    If you're looking to delete the sublist items from the original list, you can then do:

    // list is our original list
    // sublist is our (newly created) sublist built from GetRange()
    foreach (Type t in sublist)
    {
        list.Remove(t);
    }
    

提交回复
热议问题