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

  • Home
  • SEARCH
  • 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 3283934
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T20:05:52+00:00 2026-05-17T20:05:52+00:00

I am trying to execute the following code and I keep getting an Index

  • 0

I am trying to execute the following code and I keep getting an Index out of range exception when trying to assign array values to the list:-

        int[] array = new int[1000000];
        for (int i = 0; i < array.Length; i++)
        {
            array[i] = i;
        }

        List<int> list = new List<int>();
        Parallel.For(0, array.Length, i => list.Add(array[i]));

Am I doing something wrong here ? I understand that the process is unordered/asynchronous, but why does “i” get values that are higher than the value of “array.Length” ?

  • 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-17T20:05:53+00:00Added an answer on May 17, 2026 at 8:05 pm

    The problem is that you cannot call List.Add() concurrently on multiple threads. If you need thread-safe collections, see the System.Collections.Concurrent namespace.

    If you break into the debugger when you get an exception, you’ll see that i is not greater than array.Length, but is instead a power of 2 that is substantially less than array.Length. What happens is that the List starts out with an empty array of something like 4 elements. Whenever you add an element to a list whose array is full, it creates an array of twice the length of the old array, copies the old elements to it, and stores the new array.

    Now let’s say that your list is up to 31 elements (meaning it has space for one more) and two threads try to add a 32nd element. They will both execute code like this:

    if (_size == _items.Length)
    {
        EnsureCapacity(_size + 1);
    }
    _items[_size++] = item;
    

    First they will both see that _size (31) is not _items.Length (32), so they both execute _size++. The first thread will get 31 (the correct index of the 32nd element) and change _size to 32. The second thread will get 32 and try to index _items[32], which gives you your exception because it’s trying to access the 33rd element of a 32-element array.

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

Sidebar

Related Questions

I'm trying to execute an Insert statement, but keep getting a Invalid object name
When I am trying to execute following code to email the contact form details,
I am trying to execute the following code. The code tries to parallely download
So, we are trying to execute the following code. The two if statements are
I m trying to execute some code when image is not loaded. I m
I have been trying to execute the following UNIX shell script which is not
I am trying to execute a .bat file remotely and implementing following lines of
I'm trying to get a SwingWorker to work. I've the following code at the
I'm trying to do something usefull with some itens in list in the following
I am trying to execute this SQL query prior to restoring a .BAK file

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.