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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T13:01:11+00:00 2026-05-15T13:01:11+00:00

Depending on what I know, when I define a method in C#, the local

  • 0

Depending on what I know, when I define a method in C#, the local variables in this method will be released from memory after finishing executing the block of this method [when GC wants],

but if I have an inline callback in a method, will these local variables be released from memory also ?

In the following example, the [x] variable will keep its value after finishing executing the method and the message will show the value of [x] without problem although it’s in a callback !!

    private void MyMethod()
    {
        int x = 1;
        System.Timers.Timer t = new System.Timers.Timer(1000);
        t.Elapsed += (sender, e) => MessageBox.Show((x++).ToString()); ;
        t.Start();
    }
  • 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-15T13:01:12+00:00Added an answer on May 15, 2026 at 1:01 pm

    The variable x is captured in a closure associated with the lambda function. This means that the value of x isn’t actually stored on the stack associated with the exection of MyMethod but on the heap (in an object referenced from the lambda function).

    The following example shows (roughly) how the C# compiler transforms the code:

    class $$MyMethod$$Closure { 
      public int x;
      void Function(object sender, EventArgs e) {
        MessageBox.Show((x++).ToString());
      }
    }
    
    private void MyMethod() {
        var $$closure = new $$MyMethod$$Closure();
        $$closure.x = 1; 
        System.Timers.Timer t = new System.Timers.Timer(1000); 
        t.Elapsed += $$closure.LambdaFunction; 
        t.Start(); 
    }
    

    As you can see, the x variable is now stored in an heap-allocated object (called closure). The object will be alive as long as the timer can call the method (and as long as x can be accessed), but once you remove the timer, the closure will be also garbage collected.

    It is worth noting that the compiler only captures those local variables that are actually used in the lambda function (so if you have some large data in the method, they won’t be accidentally kept alive longer than needed).

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

Sidebar

Related Questions

Does anybody know of a method for specializing a template depending on whether a
I am creating a file with the content depending on some local variables. I
I am having this unexplained ActiveRecord::Relation, undefined method error . I don't know why,
I want to know how can a retrieve a UIImage size, depending on the
Afternoon, I would like to know how i can add a class depending on
[I know this is quite subjective, and dependant on various considerations, so I'm just
This is somehow subjective depending on the target translation language, but bear with me
Consider this program: #include <stdio.h> int main() { printf(%s\n, __FILE__); return 0; } Depending
I have a method that I want to take some action depending on the
I know this maybe a too generic question but since a customer user control

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.