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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T16:48:17+00:00 2026-05-11T16:48:17+00:00

I am curious about the actual .NET implementation and the decision behind it. For

  • 0

I am curious about the actual .NET implementation and the decision behind it.

For example in Java, all captured values used in an anonymous classes are required to be final. This requirement seems to be dropped in .NET.

Also, is there a difference in an implementation of captured values for value types as opposed to reference types?

Thanks

  • 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-11T16:48:17+00:00Added an answer on May 11, 2026 at 4:48 pm

    The easiest way of finding out how it’s implemented is to try it. Write some code which uses a captured variable, compile it, then look at it in Reflector. Note that it’s the variable which is captured, not the value. That’s one of the big differences between Java and C# in this area.

    The basic idea is that each level of scope containing at least one captured variable results in a new class with fields for the variables which have been captured. If there’s more than one level, then an inner scope also has a field for the next scope out, and so on. The genuine local variables on the stack end up being references to instances of the autogenerated classes.

    Here’s an example:

    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            List<Action> actions = new List<Action>();
    
            for (int i=0; i < 5; i++)
            {
                int copyOfI = i;
    
                for (int j=0; j < 5; j++)
                {
                    int copyOfJ = j;
    
                    actions.Add(delegate
                    {
                        Console.WriteLine("{0} {1}", copyOfI, copyOfJ);
                    });
                }
            }
    
            foreach (Action action in actions)
            {
                action();
            }        
        }
    }
    

    (You get different results if you don’t take a copy of course – experiment!) This is compiled into code like this:

    using System;
    using System.Collections.Generic;
    
    class Program
    {
        static void Main()
        {
            List<Action> actions = new List<Action>();
    
            for (int i=0; i < 5; i++)
            {
                OuterScope outer = new OuterScope();
                outer.copyOfI = i;
    
                for (int j=0; j < 5; j++)
                {
                    InnerScope inner = new InnerScope();
                    inner.outer = outer;
                    inner.copyOfJ = j;
    
                    actions.Add(inner.Action);
                }
            }
    
            foreach (Action action in actions)
            {
                action();
            }        
        }
    
        class OuterScope
        {
            public int copyOfI;
        }
    
        class InnerScope
        {
            public int copyOfJ;
            public OuterScope outer;
    
            public void Action()
            {
                Console.WriteLine("{0} {1}", outer.copyOfI, copyOfJ);
            }
        }
    }
    

    Every reference to the captured variable ends up going through the instance of the generated class, so it’s not just a one-off copy. (Okay, in this case nothing else in the code uses the captured variables, but you can easily imagine it could.) Note how for any one iteration of the outer loop, the five new instances all share one instance of OuterScope. You might want to try playing with extra code in the delegate to see how that affects things – if the delegate changes copyofI that change will be seen in the next delegate; changes to copyOfJ won’t be seen because the next delegate will be using a separate instance of InnerScope.

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

Sidebar

Ask A Question

Stats

  • Questions 233k
  • Answers 233k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Partial Classes shall be your friend! (link added for those… May 13, 2026 at 5:50 am
  • Editorial Team
    Editorial Team added an answer Can be done via changing the Collation. By default it… May 13, 2026 at 5:50 am
  • Editorial Team
    Editorial Team added an answer unzip $files -d $newdir May 13, 2026 at 5:50 am

Related Questions

One thing I've always wondered about is how software patches work. A lot of
Whenever I consider algorithms/data structures I tend to replace the log(N) parts by constants.
Let's say I have a table called positions (as in job positions). On the
This question stems from watching Rasmus Lerdorf's talk from Drupalcon . This question and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.