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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:52:47+00:00 2026-05-27T17:52:47+00:00

If I have this: public string DoSomething(string arg) { string someVar = arg; DoStuffThatMightTakeAWhile();

  • 0

If I have this:

public string DoSomething(string arg)
{
    string someVar = arg;
    DoStuffThatMightTakeAWhile();
    return SomeControl.Invoke(new Func<string>(() => someVar));
}

And this method can be called concurrently from multiple threads, and one thread is stuck at DoStuffThatMightTakeAWhile, and then a second thread calls DoSomething with a different arg, will this change the value of someVar for all threads and consequently, DoSomething return the second version of someArg on both calls, or will one someVar exist for each thread?

Edit I think my Action should have been a Func so edited it.

  • 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-27T17:52:48+00:00Added an answer on May 27, 2026 at 5:52 pm

    There are a number of confusions in the answers here, mostly based upon the untruth that local variables are allocated “on the stack of the thread”. This is both false and irrelevant.

    It’s false because the local variable may be allocated on some temporary pool or the long-term storage pool; even if it is allocated on a temporary pool, that need not be stack memory; it could be a register. It’s irrelevant because who cares what pool the storage is allocated on?

    The relevant fact is that a top-level local variable is allocated per method activation. More generally, a local variable in a block is allocated once per the block being entered; a local variable declared in the body of a loop, for example, is allocated every time the loop goes around.

    So, let’s consider your question:

    This method can be called concurrently from multiple threads. If one thread is stuck at DoStuffThatMightTakeAWhile, and then a second thread calls DoSomething with a different arg, will this change the value of someVar for all threads?

    No. There is a new “someVar” for each activation of DoSomething.

    will one someVar exist for each thread?

    One “someVar” will exist for each activation. If a thread does exactly one activation then there will be exactly one someVar per thread; if a thread does a million activations then there will be a million of them.

    That said, Jon Hanna’s answer is also correct: if you make a delegate which both reads and writes a local variable, and you hand that delegate out to multiple threads, then all the activations of the delegate share the same local variable. There is no magical thread safety created for you; if you want to do that then you are responsible for ensuring thread safety.

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

Sidebar

Related Questions

If I have a method like this: public void DoSomething(int Count, string[] Lines) {
I have a method similar to this: public List<MyClass> DoSomething(string Name, string Address, string
I have an action method that looks like this: public ActionResult DoSomething(string par, IEnumerable<string>
I have this: public string Log { get { return log; } protected set
In code-behind of an ASP.NET page I have this method: public string TestFunc() {
I have this method in cs page: public String getToolTip(Object productId, Object imgBtnId) {
I have a method that looks like this: public static String escape(String text) {
Let's say I have this action in a JSF Managed Bean: public String doSomething()
I have a method, which accept a string and an iterator: public int doSomething(String
I have this Class: public class User { public string id{ get; set; }

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.