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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T13:38:20+00:00 2026-06-10T13:38:20+00:00

Assume we have such code: public class Observer { public event EventHandler X =

  • 0

Assume we have such code:

public class Observer
{
    public event EventHandler X = delegate { };
}

public class Receiver
{
    public void Method(object o) {}
}

public class Program
{
    public static void DoSomething(object a, object b, Observer observer, Receiver r)
    {
        var rCopy = r;
        EventHandler action1 = (s, e) => rCopy.Method(a);
        EventHandler action2 = (s, e) => r.Method(b);
        observer.X += action1;
        observer.X += action2;
    }

    public static void Main(string[] args)
    {
        var observer = new Observer();
        var receiver = new Receiver();
        DoSomething(new object(), new object(), observer, receiver);
    }
}

Here action1 and action2 have completely separated set of captured variables – rCopy was created especially for this. Still, compiler generates just one class to capture everything (checked generated IL). I suppose it is done for optimization reasons, but it allows very hard-to-spot memory leak bugs: if a and b captured in single class, GC is unable to collect both at least so long as any of lambdas are referenced.

Is there a way to convince compiler to produce two different capture classes? Or any reason why it cannot be done?

P.S. Somewhat more detailed, in my blog: here and here.

  • 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-10T13:38:22+00:00Added an answer on June 10, 2026 at 1:38 pm

    You have rediscovered a known shortcoming in the implementation of anonymous functions in C#. I described the problem in my blog in 2007.

    Is there a way to convince compiler to produce two different capture classes?

    No.

    Or any reason why it cannot be done?

    There is no theoretical reason why an improved algorithm for partitioning closed-over variables so that they are hoisted into different closure classes could not be devised. We have not done so for practical reasons: the algorithm is complicated, expensive to get right and expensive to test, and we have always had higher priorities. Hopefully that will change in Roslyn, but we are making no guarantees.

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

Sidebar

Related Questions

Assume we have legacy classes, that can't be modified: class Foo { public void
So assume I have one class named Program which contains my whole program of
Assume I have a controller method like this: [Audit] public JsonNetResult List(int start, int
Imagine I have a method having a contract : public void Do(string value) {
assume I have a such html order <div id=header>>Header(logo, navigation menus etc)will be here,
Assume I have a such json setting: { page:1, items:[ { name: 'item1', id:1
Assume I have 2 time intervals,such as 16:30 - 20:00 AND 15:00 - 19:00,
Assume I have an enumerable object enum and now I want to get the
I have a big object say MyApplicationContext which keeps information about MyApplication such as
consider the following test class: [TestClass] public class ExampleTests { [TestMethod] public void FileDoesNotExists()

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.