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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:20:24+00:00 2026-05-12T20:20:24+00:00

I have the following code: string acctStatus = account.AccountStatus.ToString(); if (!SettableStatuses().Any(status => status ==

  • 0

I have the following code:

string acctStatus = account.AccountStatus.ToString();
if (!SettableStatuses().Any(status => status == acctStatus))
    acctStatus = ACCOUNTSTATUS.Pending.ToString();

Note that account.AccountStatus is an enum of type ACCOUNTSTATUS. On the second line, ReSharper is giving me the warning “Access to Modified Closure” for acctStatus. When I do the recommended operation, Copy to local variable, it modifies the code to the following:

string acctStatus = realAccount.AccountStatus.ToString();
string s = acctStatus;
if (!SettableStatuses().Any(status => status == s))
    acctStatus = ACCOUNTSTATUS.Pending.ToString();

Why is this better or preferable to what I had originally?

EDIT

It also recommends Wrap local variable in array, which produces:

string[] acctStatus = {realAccount.AccountStatus.ToString()};
if (!SettableStatuses().Any(status => status == acctStatus[0]))
    acctStatus[0] = ACCOUNTSTATUS.Pending.ToString();

This seems downright wacky to me.

  • 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-12T20:20:25+00:00Added an answer on May 12, 2026 at 8:20 pm

    The reason for the warning is that inside a loop you might be accessing a variable that is changing. However, the “fix” isn’t really doing anything for you in this non-loop context.

    Imagine that you had a FOR loop and the if was inside it and the string declaration was outside it. In that case the error would be correctly identifying the problem of grabbing a reference to something unstable.

    An example of what you don’t want:

    string acctStatus
    
    foreach(...)
    {
      acctStatus = account.AccountStatus[...].ToString();
      if (!SettableStatuses().Any(status => status == acctStatus))
          acctStatus = ACCOUNTSTATUS.Pending.ToString();
    }
    

    The problem is that the closure will grab a reference to acctStatus, but each loop iteration will change that value. In that case it would be better:

    foreach(...)
    {
      string acctStatus = account.AccountStatus[...].ToString();
      if (!SettableStatuses().Any(status => status == acctStatus))
          acctStatus = ACCOUNTSTATUS.Pending.ToString();
    }
    

    As the variable’s context is the loop, a new instance will be created each time because we have moved the variable inside the local context (the for loop).

    The recommendation sounds like a bug in Resharper’s parsing of that code. However, in many cases this is a valid concern (such as the first example, where the reference is changing despite its capture in a closure).

    My rule of thumb is, when in doubt make a local.

    Here is a real world example I was bitten by:

            menu.MenuItems.Clear();
            HistoryItem[] crumbs = policyTree.Crumbs.GetCrumbs(nodeType);
    
            for (int i = crumbs.Length - 1; i > -1; i--) //Run through items backwards.
            {
                HistoryItem crumb = crumbs[i];
                NodeType type = nodeType; //Local to capture type.
                MenuItem menuItem = new MenuItem(crumb.MenuText);
                menuItem.Click += (s, e) => NavigateToRecord(crumb.ItemGuid, type);
                menu.MenuItems.Add(menuItem);
            }
    

    Note that I capture the NodeType type local, note nodeType, and HistoryItem crumb.ItemGuid, not crumbs[i].ItemGuid. This ensures that my closure will not have references to items that will change.

    Prior to using the locals, the events would trigger with the current values, not the captured values I expected.

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

Sidebar

Related Questions

I have a C# application that includes the following code: string file = relativePath.txt;
I have the following code: String inputFile = somefile.txt; FileInputStream in = new FileInputStream(inputFile);
I have the following code string three() { return three; } void mutate(string& ref)
I have the following code: String serviceType; ServiceBrowser tmpBrowser; for (String playerName: players) {
I have the following code: string[] firstArray = { bla bla, bla bla, bla
currently I have the following code: String select = qry.substring(select .length(),qry2.indexOf( from )); String[]
Follow up to this question . I have the following code: string[] names =
I have the following code: import string import random d =[random.choice(string.uppercase) for x in
I have the following code : std::string Utils::get() { std::string result; result.append(1, 'x'); result.append(1,
Suppose I have the following code: foreach(string str in someObj.GetMyStrings()) { // do some

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.