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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:19:46+00:00 2026-05-14T03:19:46+00:00

I am learning C#.Can I mean closure as a construct that can adopt the

  • 0

I am learning C#.Can I mean closure as a construct that can adopt the changes in the environment in which it is defined.

Example :

List<Person> gurus = 
new List<Person>()
                 {
                  new Person{id=1,Name="Jon Skeet"},
                  new Person{id=2,Name="Marc Gravell"},
                  new Person{id=3,Name="Lasse"}
                 };            


void FindPersonByID(int id)
{
  gurus.FindAll(delegate(Person x) { return x.id == id; }); 
}

The variable id is declared in the scope of FindPersonByID() but t we still can access
the local variable id inside the anonymous function (i.e) delegate(Person x) { return x.id == id; }

(1) Is my understanding of closure is correct ?

(2) What are the advantages can we get from closures?

  • 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-14T03:19:46+00:00Added an answer on May 14, 2026 at 3:19 am

    Yes the code inside of FindPersonByID is taking advantage of a closure by using the parameter id within the lambda expression. Strictly speaking the definitions of closures are a bit more complex but at a basic level this is correct. If you want more information on how they function I encourage you to read the following articles

    • http://csharpindepth.com/Articles/Chapter5/Closures.aspx
    • http://blogs.msdn.com/jaredpar/archive/tags/Closures/default.aspx

    The primary advantage of closures is essentially what you demonstrated above. It allows you to write the code in a more natural, straight forward fashion without having to worry about the implementation details of how the lambda expression is generated (generally)

    Consider for example how much code you would have to write in the abscence of closures

    class Helper {
      private int _id;
      public Helper(int id) { 
        _id = id;
      }
      public bool Filter(Person p) {
        return p.id == _id;
      }
    }
    
    void FindPersonsByID(int id) {
      Helper helper = new Helper(id);
      gurus.FindAll(helper.Filter);
    }
    

    All of this just to express the concept of using a parameter inside of a delegate.

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

Sidebar

Related Questions

I'm learning ML, can somebody please explain what does it mean exhaustive patterns?
Where can I find a good tutorial on learning ASP.NET MVC using VB.net 2008
Can anyone recommend any good resources for learning C++ Templates? Many thanks.
Can you recommend any resources for learning about Windows Installer? The best book I've
I've just started learning Lisp and I can't figure out how to compile and
What is JDBC and where can I start learning about? I know it's a
I want to start learning HTML and AJAX using a Linux distribution. Can anyone
In Jesse Liberty's Learning C# book, he says "Objects of one type can be
I would like to start learning SQL Server 2005 on my own. Can anyone
I would be extremely appreciative if anybody can help me. I am learning C++

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.