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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T02:45:26+00:00 2026-06-05T02:45:26+00:00

I am initializing a mutable class instance as a local variable with new keyword.

  • 0

I am initializing a mutable class instance as a local variable with new keyword. Then I pass this object as a parameteter to a delegate. Is this variable’s lifetime extended by the delegate? Do other threads use this variable or create their own instances? I may be asking the obvious but I want to be sure.

public void DoSometing(Action<Foo> action)
{
    Foo foo = new Foo();
    action.Invoke(foo);
}
  • 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-05T02:45:27+00:00Added an answer on June 5, 2026 at 2:45 am

    Whenever you pass local variables that “escape” the method one way or another, you do extend its lifetime. In C# you will never operate upon a variable that contains a reference to a non-existant object — the concept makes no sense in a managed environment.

    So yes, foo will continue to live on, and you will need to be concerned with thread-safety in exactly the same way as if you simply called another ordinary method. In this scenario, lambdas do not change the complexion of the problem.

    However, sometimes this can be more subtle, especially if you return a lambda — one which closes over local variables. In such a scenario, all the variables you reference from within the lambda live on in the same way as foo.

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

Sidebar

Related Questions

I see this idiom of initializing instance variables quite a bit public class Test{
I'm initializing Entity Framework Object context, and this gives me the keyword not supported
I am initializing a mutable string and then logging it as follows. NSMutableString* mStr
When initializing a new Date object in JavaScript using the below call, I found
I'm initializing an instance of a class that tests the equality of two formulas.
I am initializing an array like this: public class Array { int data[] =
I have a doubt about initializing string with synthesize keyword. In my Event.h class
Suppose I have this class: type Pet (name:string) as this = let mutable age
I was initializing the object in the code below using simple properties but then
When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private:

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.