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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:30:06+00:00 2026-05-18T06:30:06+00:00

What is the difference between: new Thread(new ThreadStart(SomeFunc)) and: new Thread( delegate() { SomeFunc();}

  • 0

What is the difference between:

new Thread(new ThreadStart(SomeFunc))

and:

new Thread( delegate() { SomeFunc();} )

This code gives strange outputs on my computer:

public class A
{
    int Num;

    public A(int num)
    {
        Num = num;
    }

    public void DoObj(object obj)
    {
        Console.Write(Num);
    }

    public void Do()
    {
        Console.Write(Num);
    }
}

/////// in void main()

for (int i = 0; i < 10; i++)
{
    (new Thread(new ThreadStart((new A(i)).Do))).Start(); // Line 1
    (new Thread(new ThreadStart(delegate() { (new A(i)).Do(); }))).Start(); // Line 2
    (new Thread(delegate() { (new A(i)).Do(); })).Start(); // Line 3
}

If only Line 1 is executed the output is something like:

0 2 3 1 5 6 4 7 8 9

which is ok but if Line 2 or 3 is executed, output is:

3 3 3 5 5 7 7 9 9 10

There are some multiple numbers and a 10 which is quite strange that the loop is never run with the number 10. What is the trick behind these?

Thanks.

  • 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-18T06:30:06+00:00Added an answer on May 18, 2026 at 6:30 am

    With the delegate, you are capturing i.

    The difference is that with new ThreadStart((new A(i)).Do)), you are creating a new instance of A in the for loop with i as a parameter. That means that at that point, the value of i is taken and send to the constructor. The delegate you are sending is thus not of the creation of A, but you are actually sending the a delegate of the Do method of the instance of A to the constructor.

    However, with delegate() { (new A(i)).Do(); }) (both of them), you are sending a reference of i to the thread.

    The thread then takes some time to start and meanwhile, the for loop goes on. By the time i is used in the delegate (i.e. the thread has started), the for loop has moved on to 3 and that’s what you see. The same goes for the second and third thread. The three threads are started but wait for the starting thread to complete some work. Then the created threads kick in (thread 1, 2 and 3) and they do their work. The Windows goes back to the thread with the for loop and goes on to start thread 4 and 5.

    Some reading material:

    • http://www.digitallycreated.net/Blog/34/variable-capture-in-c#-with-anonymous-delegates
    • https://ericlippert.com/2009/11/12/closing-over-the-loop-variable-considered-harmful-part-one/
    • https://www.albahari.com/threading/#_Passing_Data_to_a_Thread
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there any essential difference between this code: ThreadStart starter = new ThreadStart(SomeMethod); starter.Invoke();
What is the difference between using a new thread and using a thread from
What is the difference between this: this.btnOk.Click += new System.EventHandler(this.btnOK_Click); and this? this.btnOk.Click +=
What is the difference between: Starting a new thread Using TPL Using BackgroundWorker All
Does an asynchronous call always create a new thread? What is the difference between
Well title says it, what is the difference between Executors.newSingleThreadExecutor().execute(command) and new Thread(command).start();
What is the difference between new / delete and malloc / free ? Related
What is the difference between old style and new style classes in Python? When
I was just wondering if there is any difference between the two different new
I am new to COM and trying to understand the difference between STA and

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.