Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7705329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:50:27+00:00 2026-05-31T23:50:27+00:00

I have a list which contains some items of type string. List<string> lstOriginal; I

  • 0

I have a list which contains some items of type string.

List<string> lstOriginal;

I have another list which contains idices which should be removed from first list.

List<int> lstIndices;

I’d tried to do the job with RemoveAt() method ,

foreach(int indice in lstIndices)
{
     lstOriginal.RemoveAt(indice);
}

but it crashes and said me that “index is Out of Range.”

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T23:50:28+00:00Added an answer on May 31, 2026 at 11:50 pm

    You need to sort the indexes that you would like to return from largest to smallest in order to avoid removing something at the wrong index.

    foreach(int indice in lstIndices.OrderByDescending(v => v))
    {
         lstOriginal.RemoveAt(indice);
    }
    

    Here is why: let’s say have a list of five items, and you’d like to remove items at indexes 2 and 4. If you remove the item at 2 first, the item that was at index 4 would be at index 3, and index 4 would no longer be in the list at all (causing your exception). If you go backwards, all indexes would be there up to the moment when you’re ready to remove the corresponding item.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an List<int> which contains 1,2,4,7,9 for example. I have a range from
I have a generic List (of Foo) which contains n objects of Type Foo.
I have a List which contains a list of objects and I want to
I have a Request object which contains a list of Approvers. An approver has
I have a base class Node which contains a list of child nodes. Node
I have read the page in Emacs wiki which contains a list of session
I have an array that contains a list of vertices which I copy to
LinQ contains the method Cast which casts each entry in the list to type
I have a piece of code which combines an in-memory list with some data
I have a class which contains a List<Item> I have a getter on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.