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 7929125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T19:58:19+00:00 2026-06-03T19:58:19+00:00

I have this code: List<string> list = new List<string>(30); list.Insert(1, string 1); list.Insert(10, string

  • 0

I have this code:

List<string> list = new List<string>(30);

list.Insert(1, "string 1");
list.Insert(10, "string 10");
list.Insert(5, "string 5");

The run-time is crashing on sting 10 with exception Index must be within the bounds of the List

What I can’t understand is that I have defined a list with 30 items, so why does this crash and what is the point of defining the 30 if I can’t add an items in such way?

  • 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-06-03T19:58:21+00:00Added an answer on June 3, 2026 at 7:58 pm

    The list is still empty when initialized. The parameter ’30’ means it will simply have the capacity of 30 items.

    If you insist on using a list instead of an array, you must first add 30 items and then you can set them with their appropriate index.

    The easiest way to do this would be:

    List<string> list = new string[30].ToList();
    
    list[1] = "string 1";
    list[10] = "string 10";
    list[5] = "string 5";
    

    Also note that when you’re using Insert, it will basically Add the item to a certain index, so the number of items in the list will grow.

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

Sidebar

Related Questions

I have this code: FVDTO.setStatus(fail); List<String[]> invalidFields = new ArrayList<String[]>(); Iterator<ConstraintViolation<HazardSubmission>> iterator = cv.iterator();
I have code similar to this: public List<string> Expected = new List<string>(); public int
Lets say I have this code: public void MyMethod(string Data, List<string> InputData) { //I
I have this simple code public String toString() { **Iterator it = list.iterator();** String
I have this bit of code, public static List<string> GetSentencesFromWords(List<string> words, string fileContents) {
i have this code: var list = new List<int>(); for(int i=0;i<10;i++) list.Add(i); for(int i=0;i<10;i++)
If I have this code: <div class=lists> <a href=#new_list>New list</a> </div> AND $.ajax({ url:
I have some LINQ code that generates a list of strings, like this: var
I have this code for splitting input list into its halves. It seems to
Say I have this code - public interface ParentInterface1 { public List<? extends ChildInterface1>

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.