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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T21:34:00+00:00 2026-06-13T21:34:00+00:00

Consider the following simple example: private Action _action; public Form1() { InitializeComponent(); } private

  • 0

Consider the following simple example:

private Action _action;

public Form1() { InitializeComponent(); }

private void button1_Click(object sender, EventArgs e)
{
    string msg = "test";
    Expression<Action> exp = () => MessageBox.Show(msg);
    _action = exp.Compile();
    msg = "testC";
}

private void button2_Click(object sender, EventArgs e)
{
    _action();
}

When button2 is clicked, I see testC.

My question is:

I want to store actions to be run later. Can I safely store the delegate that compile() creates and use it whenever I want?

My question is mostly about method arguments. I am not aware on how they are stored (by ref or by value), of if they can they be cleared by GC in the meantime. In general I doubt that this is the correct approach. Also, msdn isn’t of much help.

I am creating a JobManager that supports High priority actions to be executed. There is a stack of default actions and some high priority that need to be added. This part is where I register the high priority job (using a lambda -> get the method -> create delegate -> store it -> run in when needed). I am not sure if this is the right approach to store the action anyway.

  • 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-13T21:34:02+00:00Added an answer on June 13, 2026 at 9:34 pm

    Yes, you can.
    The lambda expression creates a closure that holds references to the local variables that it uses from the outer function.

    This closure is referenced in the delegate’s Target property.
    As long as you hold a reference to the delegate, those variables won’t be GC’d.

    For a more detailed look at how this works, see my blog post.

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

Sidebar

Related Questions

Consider following simple DAO example: public abstract class DAOFactory { public abstract AccountDAO getAccountDAO();
Consider the following simple RESTEasy (JAX-RS) service: @Path(/example-service) public interface ExampleService { @Path(/ping) @GET
Consider the following simple example: [DataContract({0}Base)] public class Base<T> where T : Entity<T> {
Please consider the following simple use case: public class Foo { public virtual int
Consider the following example code: http://code.google.com/apis/maps/documentation/javascript/examples/streetview-simple.html I can do scrollwheel: false on a mapOptions
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Please consider the following code implementing a simple MixIn : class Story(object): def __init__(self,
Consider the following simple example: f[x_] = Sin[5 x] + Sin[3 x]; p1 =
Consider the following simple example class, which has a property that exposes a modified
Consider the following simple example: One team has many players and a player may

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.