Linked Questions

9 votes
1 answer
7k views

Can someone explain "access to modified closure" in C# in simple terms? [duplicate]

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 ...
Golem's user avatar
  • 115
581 votes
12 answers
121k views

JavaScript closures vs. anonymous functions

A friend of mine and I are currently discussing what is a closure in JS and what isn't. We just want to make sure we really understand it correctly. Let's take this example. We have a counting loop ...
leemes's user avatar
  • 45.5k
101 votes
1 answer
14k views

Access to Modified Closure (2)

This is an extension of question from Access to Modified Closure. I just want to verify if the following is actually safe enough for production use. List<string> lists = new List<string>()...
faulty's user avatar
  • 8,267
15 votes
5 answers
14k views

C# lambda, local variable value not taken when you think?

Suppose we have the following code: void AFunction() { foreach(AClass i in AClassCollection) { listOfLambdaFunctions.AddLast( () => { PrintLine(i.name); } ); } } void Main() { ...
matt's user avatar
  • 4,144
38 votes
1 answer
22k views

ReSharper Warning - Access to Modified Closure

I have the following code: string acctStatus = account.AccountStatus.ToString(); if (!SettableStatuses().Any(status => status == acctStatus)) acctStatus = ACCOUNTSTATUS.Pending.ToString(); ...
Matt Grande's user avatar
  • 12.1k
26 votes
2 answers
13k views

How do I fix: Access to foreach variable in closure resharper warning?

I'm getting this ReSharper warning: Access to foreach variable in closure. May have different behaviour when compiled with different versions of compiler. This is what I'm doing: @foreach(var item ...
Esteban's user avatar
  • 3,138
12 votes
4 answers
2k views

c# declaring variables inside Lambda expressions

The following code outputs 33 instead of 012. I don't understand why a new variable loopScopedi isn't captured in each iteration rather than capturing the same variable. Action[] actions = new ...
Mouhyi's user avatar
  • 377
8 votes
3 answers
1k views

C# Access to modified closure

public virtual void OnRegistrationJoin(RegistrationJoinEventArgs e) { foreach (Mobile member in e.Team) { member.SendMessage(1161, "You join the {0}.", EventFullName); if (e....
bevacqua's user avatar
  • 48k
9 votes
2 answers
2k views

When I use threads to print things to the console, why does it produce odd results?

I've been getting into Threads lately while reading the very nice pdf from Rob Miles (here). He had an example on page 160 (2012, C# pdf) but it didn't write to the console just did empty loops. I ...
Sisyphus's user avatar
  • 698
5 votes
3 answers
2k views

C# - Index was outside the bounds of the array using same list sizes in for loop [duplicate]

I am creating a file downloader in .NET which downloads an array of files from a server using Asynchronous tasks. However, even though I create the Task[] and returned string[] with the same length. ...
Timmo's user avatar
  • 2,286
2 votes
4 answers
3k views

From Eric Lippert's blog: "don't close over the loop variable" [duplicate]

Possible Duplicates: Why is it bad to use a iteration variable in a lambda expression C# - The foreach identifier and closures From Eric Lippert's 28 June 2010 entry: static IEnumerable<...
Martijn's user avatar
  • 6,763
6 votes
1 answer
2k views

IndexOutOfRangeException exception when using tasks in for loop in C#

I am trying to use tasks in a for loop, but I am getting really strange exception! here is my code: Task[] tasks = new Task[strarrFileList.Length]; for (int ii = 0; ii < ...
NESHOM's user avatar
  • 909
4 votes
2 answers
2k views

Access to Modified Closure in Lambda Expression

foreach(var category in categories) { a.AddRange(_db.Articles.Where(c => c.Categories.Contains(category))); } The code runs fine, yet I get a warning about "access to modified closure" in ...
one.beat.consumer's user avatar
1 vote
4 answers
5k views

Why is this input value undefined with jquery

I'm creating multiple form retrieving data from jquery. after that i want to work with individual form without sending the form but, i'm having problems retrieving the value of the inputs. they are ...
Miquel Gabriel's user avatar
-1 votes
3 answers
1k views

c# foreach with Action.BeginInvoke

Alright, so I'm having a bit of an issue here. Here is the loop. lock (ClientLocker) { Trace.WriteLine("#WriteAll: " + sm.Header); foreach (Client c in Clients) { if (c.LoggedIn) ...
Kelly Elton's user avatar
  • 4,387

15 30 50 per page