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 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
  • 1 View
  • 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

Related Questions

I'm curious about the underlying implementation of static variables within a function. If I
I'm curious about the possibility of having a .NET class library that provides a
Curious about user notification techniques. Referring to simple stuff. For example, a user submit's
What I'm curious about, is I have a site (ASP.NET), which will display some
I'm curious about the possibility to alter href attributes of all generated links. Let's
I use ASP.NET MVC with C# for web development and I am curious about
I am curious about MySQL ENUM datatype. For example if I have a few
Curious about running multiple xvfb displays: I have between 10-50 instances of a script
Being curious about how maven works exactly, I've checked out its source codes. Astonishingly,
I'm curious about the correct way to architect an application that consists of the

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.