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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:05:11+00:00 2026-06-07T22:05:11+00:00

Possible Duplicate: What is a 'Closure'? Access to Modified Closure Access to Modified Closure

  • 0

Possible Duplicate:
What is a 'Closure'?
Access to Modified Closure
Access to Modified Closure (2)

Resharper is complaining about the following piece of code:

foreach (string data in _dataList)
    DoStuff (() => field);

What is a closure? And why should I care?

I’ve read about closure in maths and functional programming and I’m at a loss here. Too heavy for my brain.

In simpler terms, what is going on here?

  • 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-07T22:05:13+00:00Added an answer on June 7, 2026 at 10:05 pm

    Here is a fairly good explanation.

    A closure is created when you reference a variable in the body of a method from a delegate. Essentially, a class that contains a reference to the local variable is generated.
    If the variable is constantly modified, when an external method calls the delegate, it may contain an unpredictable value, or even throw an exception.
    For example, in an example like this:

    foreach (string data in _dataList)
    {
        DoStuff (() => data);
    }
    

    The method () => data is always going to be the same method. if you store it, you don’t know what happens when it’s eventually invoked — what will the value of data be at the time? Will it even be valid? This is especially dangerous if you use yield return.

    A simpler example, without an iterator, is:

    var x = 5;
    Action f = () => Console.WriteLine(x);
    x = 76;
    f();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: What is a ‘Closure’? I read more and more about closures but
Possible Duplicate: Why doesn't this closure have access to the 'this' keyword? - jQuery
Possible Duplicate: Is it possible to access outer local variable in PHP? PHP closure
Possible Duplicate: How do you evaluate a string as a clojure expression? Getting data
Possible Duplicate: JavaScript scope and closure What is this for? (function(){ //The code to
Possible Duplicate: What is the 'new' keyword in JavaScript? creating objects from JS closure:
Possible Duplicate: && operator in Javascript In the sample code of the ExtJS web
Possible Duplicate: regex for URL including query string I have a text or message.
Possible Duplicate: Event handlers inside a Javascript loop - need a closure? I have
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in

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.