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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:58:16+00:00 2026-06-05T20:58:16+00:00

Possible Duplicate: C# Captured Variable In Loop I’m working on a few simple applications

  • 0

Possible Duplicate:
C# Captured Variable In Loop

I’m working on a few simple applications of threading, but I can’t seem to get this to work:

class ThreadTest
{
    static Queue<Thread> threadQueue = new Queue<Thread>();

    static void Main()
    {
        //Create and enqueue threads
        for (int x = 0; x < 2; x++)
        {
            threadQueue.Enqueue(new Thread(() => WriteNumber(x)));
        }

        while(threadQueue.Count != 0)
        {
            Thread temp = threadQueue.Dequeue();
            temp.Start();
        }

        Console.Read();
    }

    static void WriteNumber(int number)
    {
        for (int i = 0; i < 1000; i++)
        {
            Console.Write(number);
        }
    }
}

The goal basically is to add threads to a queue one by one, and then to go through the queue one by one and pop off a thread and execute it. Because I have “x<2” in my for loop, it should only make two threads – one where it runs WriteNumber(0), and one where it runs WriteNumber(1), which means that I should end up with 1000 0’s and 1000 1’s on my screen in varying order depending on how the threads are ultimately executed.

What I end up with is 2000 2’s. The two possible solutions that I’ve come up with are: I’ve missed something glaringly obvious, or sending the variable x to the WriteNumber function is doing a pass-by-reference and not pass-by-value, so that when the threads execute they’re using the most recent version of x instead of what it was at the time that the function was set. However, it was my understanding that variables are passed by value by default in C#, and only passed by reference if you include ‘ref’ in your parameter.

  • 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-05T20:58:18+00:00Added an answer on June 5, 2026 at 8:58 pm

    You’re capturing x in the lambda expression. The value of x changes to 2 before you start the threads. You need to make a copy of value within the loop:

    for (int x = 0; x < 2; x++)
    {
        int copy = x;
        threadQueue.Enqueue(new Thread(() => WriteNumber(copy)));
    }
    

    Lambda expressions capture variables. Even though the value passed to WriteNumber is passed by value, it’s not called at all until the thread is started – by which time x is 2.

    By making a copy within the loop, each iteration of the loop gets its own separate “instance” of the copy variable, and that doesn’t change value… so by the time WriteNumber is called, each copy variable still has the same value that x had for that iteration.

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

Sidebar

Related Questions

Possible Duplicate: C# Captured Variable In Loop I am pretty new to multi-threading programming.
Possible Duplicate JavaScript: Changing src-attribute of a embed-tag but this is not working for
Possible Duplicate: Can colorized output be captured via shell redirect? setup In this case
Possible Duplicate: How can I convert a list<> to a multi-dimensional array? I want
Possible Duplicate: How can I understand nested ?: operators in PHP? Why does this:
Possible Duplicate: Can main function call itself in C++? I found this problem very
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: How can I combine multiple rows into a comma-delimited list in Oracle?
Possible Duplicate: How can I remove external links from HTML using Perl? Alright, i'm
Possible Duplicate: Can a Bash script tell what directory it's stored in? Is there

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.