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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:33:27+00:00 2026-05-19T12:33:27+00:00

I was reading this question, and read this response This is actually a fantastic

  • 0

I was reading this question, and read this response

This is actually a fantastic feature.
This lets you have a closure that
accesses something normally hidden,
say, a private class variable, and let
it manipulate it in a controlled way
as a response to something like an
event.

You can simulate what you want quite
easily by creating a local copy of the
variable, and using that.

Would we need to implement Lock() in this situation?

What would that look like?

According to Eric Lippert a compiler makes code look like this:

private class Locals
{
  public int count;
  public void Anonymous()
  {
    this.count++;
  }
}

public Action Counter()
{
  Locals locals = new Locals();
  locals.count = 0;
  Action counter = new Action(locals.Anonymous);
  return counter;
}

What does the Lambda would look like, as well as the long-form code?

  • 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-19T12:33:28+00:00Added an answer on May 19, 2026 at 12:33 pm

    If you have a reason to lock, then yes, there’s nothing stopping you from putting a lock statement in a closure.

    For example, you could do this:

    public static Action<T> GetLockedAdd<T>(IList<T> list)
    {
        var lockObj = new object();
        return x =>
        {
            lock (lockObj)
            {
                list.Add(x);
            }
        }
    }
    

    What does this look like, in terms of compiler-generated code? Ask yourself: what is captured?

    • A local object used for locking.
    • The IList<T> passed in.

    These will be captured as instance fields in a compiler-generated class. So the result will look something like this:

    class LockedAdder<T>
    {
        // This field serves the role of the lockObj variable; it will be
        // initialized when the type is instantiated.
        public object LockObj = new object();
    
        // This field serves as the list parameter; it will be set within
        // the method.
        public IList<T> List;
    
        // This is the method for the lambda.
        public void Add(T x)
        {
            lock (LockObj)
            {
                List.Add(x);
            }
        }
    }
    
    public static Action<T> GetLockedAdd<T>(IList<T> list)
    {
        // Initializing the lockObj variable becomes equivalent to
        // instantiating the generated class.
        var lockedAdder = new LockedAdder<T> { List = list };
    
        // The lambda becomes a method call on the instance we have
        // just made.
        return new Action<T>(lockedAdder.Add);
    }
    

    Does that make sense?

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

Sidebar

Related Questions

NOTE: Before reading this question please note that I have read the previous questions
I was reading this question (which you do not have to read because I
Having read this question Immutable or not immutable? and reading answers to my previous
Now that it's answered: Don't bother reading this question, it's a bit lengthy and
I have been reading this question and doesn't get the problem it encounters. why
So, a while back I read a joke that went something like this: Never
After reading this question, I need to clear up some things. IQueryable<Customer> custs =
After reading this question , i saw the answer by Naveen containing a link
I was reading this question, but didn't understand some part of the aix's answer
I was reading over this question and wondered if the accepted answer might also

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.