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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:32:56+00:00 2026-05-11T05:32:56+00:00

In the two following snippets, is the first one safe or must you do

  • 0

In the two following snippets, is the first one safe or must you do the second one?

By safe I mean is each thread guaranteed to call the method on the Foo from the same loop iteration in which the thread was created?

Or must you copy the reference to a new variable ‘local’ to each iteration of the loop?

var threads = new List<Thread>(); foreach (Foo f in ListOfFoo) {           Thread thread = new Thread(() => f.DoSomething());     threads.Add(thread);     thread.Start(); } 

–

var threads = new List<Thread>(); foreach (Foo f in ListOfFoo) {           Foo f2 = f;     Thread thread = new Thread(() => f2.DoSomething());     threads.Add(thread);     thread.Start(); } 

Update: As pointed out in Jon Skeet’s answer, this doesn’t have anything specifically to do with threading.

  • 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. 2026-05-11T05:32:56+00:00Added an answer on May 11, 2026 at 5:32 am

    Edit: this all changes in C# 5, with a change to where the variable is defined (in the eyes of the compiler). From C# 5 onwards, they are the same.


    Before C#5

    The second is safe; the first isn’t.

    With foreach, the variable is declared outside the loop – i.e.

    Foo f; while(iterator.MoveNext()) {      f = iterator.Current;     // do something with f } 

    This means that there is only 1 f in terms of the closure scope, and the threads might very likely get confused – calling the method multiple times on some instances and not at all on others. You can fix this with a second variable declaration inside the loop:

    foreach(Foo f in ...) {     Foo tmp = f;     // do something with tmp } 

    This then has a separate tmp in each closure scope, so there is no risk of this issue.

    Here’s a simple proof of the problem:

        static void Main()     {         int[] data = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };         foreach (int i in data)         {             new Thread(() => Console.WriteLine(i)).Start();         }         Console.ReadLine();     } 

    Outputs (at random):

    1 3 4 4 5 7 7 8 9 9 

    Add a temp variable and it works:

            foreach (int i in data)         {             int j = i;             new Thread(() => Console.WriteLine(j)).Start();         } 

    (each number once, but of course the order isn’t guaranteed)

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

Sidebar

Related Questions

I have the two following text files: First one: chr10 1000 1001 DEL 2.4807
The following are two xaml snippets where the sole difference is that one example
Following piece of code produces two alert boxes, first one containing undefined , and
Consider the following two snippets: Exhibit A : template<typename CalcFuncT> int perform_calc(CalcFuncT&& calcfunc) {
What is the difference between the following two snippets of code: using (Object o
I have the following code snippet: enum { one } x; enum { two
Consider the two following regular expression snippets and dummy HTML that it should be
Is there any practical difference between the following two code snippets: NSObject * obj
can somebody please tell me the difference between the following two code snippets: //Code
In two different views I have the following two pieces of code. FIRST <table

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.