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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:41:00+00:00 2026-05-26T10:41:00+00:00

Simple copy pasta here: static void Main(string[] args) { List<Task> Tasks = new List<Task>();

  • 0

Simple copy pasta here:

static void Main(string[] args)
{
    List<Task> Tasks = new List<Task>();

    Random r = new Random();

    for (int o = 0; o < 5; o++)
        Tasks.Add(Task.Factory.StartNew(() => { int i = r.Next(0, 3000); Thread.Sleep(i); Console.WriteLine("{0}: {1}", o, i); }));

    Task.WaitAll(Tasks.ToArray());

    Console.Read();
}

When you run that, you will get something like this:

5: 98
5: 198
5: 658
5: 1149
5: 1300

What am I not understanding about this? Writing each iteration of o is showing as 5 for all threads when I’d expect to see numbers 0 through 4 in random order.

I tried using an actual method instead of anonymous and it does the same thing. What am I missing?

Edit: I just found the problem with my very first post and edited the question, so sorry if you answered about the improper order problem. However, I am curious as to why o is not writing properly.

  • 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-26T10:41:00+00:00Added an answer on May 26, 2026 at 10:41 am
    () => 
     { 
       int i = r.Next(0, 3000); 
       Thread.Sleep(i); 
       Console.WriteLine("{0}: {1}", o, i); 
     })
    

    Your are closing over your loop variable o with the delegate you use for your task – by the time it is executed your loop has finished and you only get the end value 5 for o. Remember you are creating a closure over the loop variable, not it’s current value – the value is only evaluated when the delegate is executed once the task is started.

    You have to create a local copy of the loop variable instead, which you can then use safely:

    for (int o = 0; o < 5; o++)
    {
       int localO = o;
       Tasks.Add(Task.Factory.StartNew(() => { int i = r.Next(0, 3000); Thread.Sleep(i); Console.WriteLine("{0}: {1}", localO, i); }));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here is a website with a list of books in a relatively simple format.
Whenever I try to copy an entire working copy using simple drag and drop
Is there a simple way to create a copy of a database or schema
Is there a simple way to accept the working copy as is in Subversion?
I'm writing a simple ruby sandbox command-line utility to copy and unzip directories from
Is there a simple way for subclasses of UITableViewCell to show the 'Copy' UIMenuController
I'm trying to write a simple ruby script that will copy a file to
I have following simple class: @interface Article: NSObject { NSString *title; } @property (copy,
I think this is a simple question. How do I disable copy from a
I've got IIS7 installed and have a simple website at: c:/inetpub/wwwroot/testsite I can copy

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.