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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:33:18+00:00 2026-05-10T20:33:18+00:00

I have seen samples of closure from – What is a 'Closure'? Can anyone

  • 0

I have seen samples of closure from – What is a 'Closure'?

Can anyone provide simple example of when to use closure?

Specifically, scenarios in which closure makes sense?

Lets assume that the language doesn’t have closure support, how would one still achieve similar thing?

Not to offend anyone, please post code samples in a language like c#, python, javascript, ruby etc.
I am sorry, I do not understand functional languages yet.

  • 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. 2026-05-10T20:33:19+00:00Added an answer on May 10, 2026 at 8:33 pm

    Closures are simply great tools. When to use them? Any time you like… As has already been said, the alternative is to write a class; for example, pre C# 2.0, creating a parameterised thread was a real struggle. With C# 2.0 you don’t even need the `ParameterizedThreadStart’ you just do:

    string name = // blah int value = // blah new Thread((ThreadStart)delegate { DoWork(name, value);}); // or inline if short 

    Compare that to creating a class with a name and value

    Or likewise with searching for a list (using a lambda this time):

    Person person = list.Find(x=>x.Age > minAge && x.Region == region); 

    Again – the alternative would be to write a class with two properties and a method:

    internal sealed class PersonFinder {     public PersonFinder(int minAge, string region)     {         this.minAge = minAge;         this.region = region;     }     private readonly int minAge;     private readonly string region;     public bool IsMatch(Person person)     {         return person.Age > minAge && person.Region == region;     } } ... Person person = list.Find(new PersonFinder(minAge,region).IsMatch); 

    This is fairly comparable to how the compiler does it under the bonnet (actually, it uses public read/write fields, not private readonly).

    The biggest caveat with C# captures is to watch the scope; for example:

            for(int i = 0 ; i < 10 ; i++) {             ThreadPool.QueueUserWorkItem(delegate             {                 Console.WriteLine(i);             });         } 

    This might not print what you expect, since the variable i is used for each. You could see any combination of repeats – even 10 10’s. You need to carefully scope captured variables in C#:

            for(int i = 0 ; i < 10 ; i++) {             int j = i;             ThreadPool.QueueUserWorkItem(delegate             {                 Console.WriteLine(j);             });         } 

    Here each j gets captured separately (i.e. a different compiler-generated class instance).

    Jon Skeet has a good blog entry covering C# and java closures here; or for more detail, see his book C# in Depth, which has an entire chapter on them.

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

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer You are in the right track. That's the way to… May 11, 2026 at 8:58 am
  • added an answer The control you are looking for is called a suggester.… May 11, 2026 at 8:58 am
  • added an answer Try using an abstract class for your domain object. class… May 11, 2026 at 8:58 am

Related Questions

I have seen samples of closure from - What is a 'Closure'? Can anyone
Most of the MVC samples I have seen pass an instance of the view
I have seen sample source code around that uses different ways of releasing/dealloc'ing objects,
Every sample that I have seen uses static XML in the xmldataprovider source, which
I have seen examples of printing from a windows application but I have not
I have seen lots of questions recently about WPF... What is it? What does
I have seen Solutions created in Visual Studio 2008 cannot be opened in Visual
I have seen simple example Ajax source codes in many online tutorials. What I
I have seen the references to VistaDB over the years and with tools like
I have seen this problem arise in many different circumstances and would like to

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.