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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:47:28+00:00 2026-06-05T12:47:28+00:00

static void Main(string[] args) { var s = 3; Func<int, Func<int>> func = x

  • 0
static void Main(string[] args)
{
    var s = 3;

    Func<int, Func<int>> func = 
        x => () =>
    {
        return x;
    };

    var result1 = func(s);

    func = null;

    s = 5;

    var result2 = result1();

    Console.WriteLine(result2);


    Console.ReadKey();
}

My understanding is that x is not actually declared as a variable eg. var x = 3. Instead, it’s passed into the outer function, which returns a function that returns the original value. At the time it is returning this, it creates a closure around x to remember its value. Then later on, if you alter s, it has no effect.

Is this right?

(Output is 3 by the way, which I’d expect).

Edit: here’s a diagram as to why I think it is

Closure example

x=3 is passed into the func, and it returns a function that simply returns x. But x doesn’t exist in the inner function, only its parent, and its parent no longer exists after I make it null. Where is x stored, when the inner function is ran? It must create a closure from the parent.

Further clarification:

    int s = 0;

    Func<int, Func<int>> func = 
        x => () =>
    {
        return x;
    };

    for (s = 0; s < 5; s++)
    {
        var result1 = func(s);

        var result2 = result1();

        Console.WriteLine(result2);
    };

Output is 0, 1, 2, 3, 4

However with your example:

static void Main(string[] args)
{
    int s = 0;

    Func<int, Func<int>> func = 
        x => () =>
    {
        return s;
    };

    List<Func<int>> results = new List<Func<int>>();

    for (s = 0; s < 5; s++)
    {
         results.Add(func(s));
    };

    foreach (var b in results)
    {
        Console.WriteLine(b());
    }

    Console.ReadKey();
}

The output is 5 5 5 5 5, which isn’t what you want. It hasn’t captured the value of the variable, it’s merely retained a reference to the original s.

Closures are created in javascript precisely to avoid this problem.

  • 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-05T12:47:31+00:00Added an answer on June 5, 2026 at 12:47 pm

    No, that’s not a closure on s, x is just an argument to the lambda expression which generates a delegate (which could actually be simplified to x => () => x.

            Func<int, Func<int>> func = x => () => x;
    
            var a = func(3);
    
            var b = func(5);
    
            Console.WriteLine(a());
            Console.WriteLine(b());
    

    You’ll get 3 and 5 as expected. In neither case does it actually remember x from the other time, but it does close on x locally during the duration of the outer lambda.

    Basically, each call to x => () => x will create a new () => x delegate which captures the local value of x (not of the s passed in).

    Even if you use s and pass it in, and change it, you still get 3 and 5:

            int s = 3;
            Func<int, Func<int>> func = x => () => x;
    
            var a = func(s);
    
            s = 5;
    
            var b = func(s);
    
            // 3 and 5 as expected
            Console.WriteLine(a());
            Console.WriteLine(b());
    

    Now, it does capture on the local variable x within the local function, which is generated at every call. So the x won’t persist between invocations of the larger lambda, but it will be captured if changed later in the lambda.

    For example, let’s say instead you had this:

            int s = 3;
            Func<int, Func<int>> func = x => 
                () => {
                          Func<int> result = () => x;
                          x = 10 * x;
                          return result;
                       };
    
            var a = func(s);
    
            s = 5;
    
            var b = func(s);
    

    In this case, the closure on x within the anonymous method is more obvious. The results of running this will be 30 and 50 because modifications to x within the anonymous method affect the closure on the x local to that anonymous method, however, these do not carry over between calls, because it’s only capturing the local x passed into the anonymous method, and not the s used to call it.

    So, to sum up, in your diagram and example:
    * main passes s into the larger lambda (func in your diagram)
    * func closes on x at the time it’s called to generate the anonymous method () => x

    Once the call to the outer lambda is made (func) the closer begins and ends because it’s local to that lambda only and doesn’t close on anything from main.

    Does that help?

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

Sidebar

Related Questions

class Program { static void Main(string[] args) { var dictionary = new Dictionary<string, int>()
First look at this code: class Program { static void Main(string[] args) { var
I've got really simple code: static void Main(string[] args) { var task = Task.Factory.StartNew(GetInt);
I build some validation service. private static void Main(string[] args) { var service =
public static void main(String[] args) { int i = 0; while(i==0){ System.out.println(Possibilities: R, T,
I have the following code static void Main(string[] args) { //var source = BlockingMethod();
This is my test code: class PassingInActionStatement { static void Main(string[] args) { var
Consider this code: static void Main(string[] args) { var persons = new List<Person> {
class Program { static void Main(string[] args) { var getfiles = new fileshare.Program(); string
Given the following sample code; class Program { static void Main(string[] args) { var

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.