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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T08:26:51+00:00 2026-05-27T08:26:51+00:00

Similar to how lambda expressions with free variables work I would like to implement

  • 0

Similar to how lambda expressions with free variables work I would like to implement my own closure class that captures some method parameter.

public class Closure<TObject, TVariable>
{
    public TVariable Variable { get; set; }
    public Func<TObject, bool> Predicate { get; set; }
}

Then I have some class that works with DateTime instances. One of the methods that should use this closure class is this:

// Original Version
public IEnumerable<Item> GetDayData(DateTime day)
{
    this.items.Where(i => i.IsValidForDay(day));
}

I would like to convert it to use Closure class. The problem is that I would like to reuse (performance reasons) my Closure class instance:

private Closure<Item, DateTime> closure = null;
public IEnumerable<Item> GetDayData(DateTime day)
{
    if (closure == null)
    {
        this.closure = new Closure<Item, DateTime>() {
            Variable = reference of "day" param, <=== HOW ????????
            Predicate = i => i.IsValidForDay(this.closure.Variable)
        }
    }
    this.items.Where(this.closure.Predicate);
}

In order for me to reuse the same Closure instance I would have to not store the value of the parameter (into closure’s Variable field) but rather method parameter’s reference pointer. So the next time this method gets called closure’s Variable would actually point to the correct value to be used.

How am I supposed to do this?

A similar thing is done when compiler generates classes that capture lambda expression free variables. I’ve been looking at decompiled code (using Reflector), but I don’t seem to understand how that’s done…

  • 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-27T08:26:52+00:00Added an answer on May 27, 2026 at 8:26 am

    Your example is slightly wrong (also this probably wont work, but the semantics/intent is correct):

    private Closure<Item, DateTime> closure = null;
    public IEnumerable<Item> GetDayData(DateTime day)
    {
        if (closure == null)
        {
            this.closure = new Closure<Item, DateTime>() {
                Predicate = i => i.IsValidForDay(this.closure.Variable)
            }
        }
        // assign here, else you only capture it the first time
        closure.Variable = day;
        this.items.Where(this.closure.Predicate);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Similar to the one here on StackOverFlow, I would be needing to implement a
I'd need to be able to combine 2 lambda expressions into 1: This would
I would like to know if there's any templating engine that is used for
Can lambda expressions be used as class template parameters? (Note this is a very
I had a part of code that takes in lambda expressions at runtime, which
I've recently started using new C++11 features like lambda expressions to make my code
Similar questions have been asked, but nothing exactly like mine, so here goes. We
After discovering lambda expressions, and their use as anonymous functions, I've found myself writing
I am learning scheme. I know how to use both lambda and let expressions.
I use a method similar to the following to get some precomputed metadata related

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.