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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:32:46+00:00 2026-05-23T13:32:46+00:00

Will this fail ? Resharper reports this as an instance of Access to modified

  • 0

Will this fail ?
Resharper reports this as an instance of “Access to modified Closure”
Will the lambda be triggered for every value ? Is the iterator generating the complete list of all interval values in this before the line that changes first runs? Or is the line first = itvl;
running for reach iteration, and that changed value of first used for subsequent iterations ??

 public HourInterval FirstInterval
    {
        get
        {
            var first = HourInterval.Make(DateTime.MaxValue);
            foreach (var itvl in this.Where
                        (itvl => itvl < first))
                first = itvl;
            return first;
        }
    }

NOTE. HourInterval is a value-type struct that represents each one-hour-long calendar hour… and this is an IEnumerable collection of HourInterval objects

EDIT:

The above is what Resharper suggested to convert to a LINQ expression from the below foreach construction …

    public HourInterval FirstInterval
    {
        get
        {
            var first = HourInterval.Make(DateTime.MaxValue);
            foreach (var itvl in this)
               if(itvl < first)
                  first = itvl;
            return first;
        }
    }
  • 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-23T13:32:46+00:00Added an answer on May 23, 2026 at 1:32 pm

    OK, this is a bit of a mess.

    First off, it is a poor programming practice to use the same variable name in two slightly inconsistent ways in the same code. It’s very confusing. Frankly, I would prefer this to be illegal; the reason that it is not illegal is a bit complicated; see http://blogs.msdn.com/b/ericlippert/archive/2009/11/05/simple-names-are-not-so-simple-part-two.aspx for details.

    Let’s get rid of that problem:

    var first = HourInterval.Make(DateTime.MaxValue);
    foreach (var itvl in this.Where(x => x < first))
      first = itvl;
    

    Now, the next question is: is Resharper correct to note that this is an access to a modified closure? Yes, Resharper is correct; you are modifying a closed-over variable of a lambda that will be called repeatedly. Resharper is noting that this is dangerous because Resharper does not know what “Where” does. For all Resharper knows, “Where” is caching every predicate it gets and is saving it up to execute later, in the mistaken belief that each predicate will do something different. In fact each predicate is the same, because each predicate is closed over the same variable, not closed over different variables.

    Clearly no sensible implementation of “Where” will do that. But Resharper doesn’t know that.

    The next question is: is this a sensible thing to do? No. This is a terribly unidiomatic and confusing way to implement “Min”, by modifying the closed-over variable of a predicate to “Where”. If you want to write Min, just write Min:

    static DateTime? Min(this IEnumerable<DateTime> seq)
    {
        DateTime? min = null;
        foreach(DateTime current in seq)
        {
            if (min == null || current < min.Value) 
                min = current;
        }
        return min;
    }
    

    There, that returns the earliest date in the sequence, or null if the sequence is empty. No messing about with Where and predicates and mutated closures and all that nonsense: write the code to be straightforward and obviously correct.

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

Sidebar

Related Questions

Will this work for testing whether a value at position index exists or not,
Assuming the stream extraction won't fail, will this if( !(stream >> token) ) throw
Will this work? try: try: field.value = filter(field.value, fields=self.fields, form=self, field=field) except TypeError: field.value
In What lines will this code fail (meaning: don't do what they are supposed
I have a script: #!/bin/bash SINGLE_FILE=/tmp/blah.file MULTIPLE_FILES=/tmp/{dir1,dir2}/*.file cp $SINGLE_FILE $MULTIPLE_FILES /tmp/newDir This will fail
Will this code ever wait on the mutex inside the producer's void push(data) ?
Will this validate in XHTML? <span>hello<span>world</span></span>
If I use INT(12) vs INT(10) or INT(8) what will this actually do in
I have installed an app on my iPod touch. Will this app expire when
I have a website that generates pdf file from CSP. Will this pdf file

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.