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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T17:55:44+00:00 2026-05-27T17:55:44+00:00

ReSharper 6.0 gives me the Access to modified closure warning for the dr identifier

  • 0

ReSharper 6.0 gives me the “Access to modified closure” warning for the dr identifier in the first code snippet.

private IEnumerable<string> GetTheDataTableStrings(DataTable dt) {
    foreach (DataRow dr in dt.Rows) {
        yield return GetStringFuncOutput(() => dr.ToString());
    }
}

I think I have a basic understanding of what this warning is trying to protect me from: dr changes several times before GetTheDataTableStrings’s output is interrogated, and so the caller might not get the output/behavior I expect.

But R# doesn’t give me any warning for the second code snippet.

private IEnumerable<string> GetTheDataTableStrings(DataTable dt) {
    return from DataRow dr in dt.Rows select GetStringFuncOutput(dr.ToString);
}

Is it safe for me to discard this warning/concern when using the comprehension syntax?

Other code:

string GetStringFuncOutput(Func<string> stringFunc) {
    return stringFunc();
}
  • 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-27T17:55:44+00:00Added an answer on May 27, 2026 at 5:55 pm

    First off, you are correct to be concerned about the first version. Each delegate created by that lambda is closed over the same variable and therefore as that variable changes, the meaning of the query changes.

    Second, FYI we are highly likely to fix this in the next version of C#; this is a major pain point for developers.

    (UPDATE: This answer was written in 2011. We did in fact take the fix described below in C# 5.)

    In the next version each time you run through the “foreach” loop we will generate a new loop variable rather than closing over the same variable every time. This is a “breaking” change but in the vast majority of cases the “break” will be fixing rather than causing bugs.

    The “for” loop will not be changed.

    See http://ericlippert.com/2009/11/12/closing-over-the-loop-variable-considered-harmful-part-one/ for details.

    Third, there is no problem with the query comprehension version because there is no closed-over variable that is being modified. The query comprehension form is the same as if you’d said:

    return dt.Rows.Select(dr=>GetStringFuncOutput(dr.ToString));
    

    The lambda is not closed over any outer variable, so there is no variable to be modified accidentally.

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

Sidebar

Related Questions

The following line of code gives a warning with Resharper: 'Comparison of floating point
I used to see any .net source code (like System.String) with ReSharper and Visual
When I add .ToLowerInvariant() to Path.GetExtension(file.FileName) , ReSharper gives a warning for a possible
Resharper gives a possible null reference warning for the as cast here. Is there
The following code gives me the warning Contract class 'FooContracts' should be an abstract
After upgrading to ReSharper5 it gives me even more useful tips on code improvements.
Resharper is showing a Possible System.NullReferenceException warning. I however can't see how I can
In resharper I get a blue curly line when for example my private property
I love Resharper, but sometimes it gives incorrect warnings, probably because the built-in annotations
I'm converting a nullable integer to a string , and Resharper warns me to

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.