Linked Questions
34 questions linked to/from The foreach identifier and closures
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<IEnumerable&...
2 votes
2 answers
904 views
C# thread safe closure inside foreach [duplicate]
I wrote a code block, but I am not sure it is thread safe. List<Task> tasks = new List<Task>(); foreach (KeyValuePair<string, string> kvp in result) { var t = new Task(async () =...
0 votes
2 answers
687 views
duplicate parameters in threads C# [duplicate]
Hello people I have a problem using parameters in threads. The problem is that I put an object List<object> as a parameter for a thread array in a foreach loop iterating a list of Lists (List<...
2 votes
2 answers
114 views
Threads carry duplicate string objects [duplicate]
Here is my code. The log file which is created in the startLog method has proper count from 1 to 1000 but log file being created by log method has duplicate values. For example if 88 is repeated then ...
0 votes
1 answer
80 views
C# simple multithreading - why is it not producing the correct results? [duplicate]
can someone tell me why the below is not producing the correct results? It is giving me 1233 when I expected 0123. public static readonly object locker = new object(); public static List<...
0 votes
2 answers
166 views
Lambda expression as ThreadStart strange behavior [duplicate]
Possible Duplicate: C# - The foreach identifier and closures From Eric Lippert’s blog: “don’t close over the loop variable” I'm using a lambda expression as ThreadStart parameter, to run a ...
0 votes
0 answers
71 views
Passing object into Thread [duplicate]
I have a question on this program. It does not produce the correct result. I assume that it should give me output like + 1 + 2 - 1 + 3 - 2 + 4 - 3 + 5 - 4 - 5 (not order specific). However, it outputs ...
1 vote
1 answer
57 views
Starting a job in a thread reads from queue from other thread [duplicate]
I have a problem that I can't figure out. It might not be easy to explain. I have a singleton class with this private constructor: private BarcodeMonitor() { processors[Machines.H1] = new ...
227 votes
12 answers
54k views
What are 'closures' in .NET?
What is a closure? Do we have them in .NET? If they do exist in .NET, could you please provide a code snippet (preferably in C#) explaining it?
36 votes
5 answers
11k views
F# vs IronPython: When is one preferred to the other?
While the languages F# and IronPython are technically dissimilar, there is large overlap between their potential uses in my opinion. When is one more applicable than the other? So far it look to me ...
41 votes
1 answer
7k views
Has foreach's use of variables been changed in C# 5?
In this answer https://stackoverflow.com/a/8649429/1497 Eric Lippert says that "FYI we are highly likely to fix this in the next version of C#; this is a major pain point for developers" with regards ...
8 votes
6 answers
2k views
Strange things in JavaScript "for"
I'm using jQuery and I have a strange thing that I don't understand. I have some code: for (i = 1; i <= some_number; i++) { $("#some_button" + i).click(function() { alert(i); }); } ...
7 votes
2 answers
2k views
Anonymous method for event handler in for loop
Can this be done in a for loop? TickEventArgs targs1 = new TickEventArgs(lbl1_up_time, _elapsedTime_up1); timer_up1.Tick += (sender, e) => Tick(targs1); TickEventArgs ...
1 vote
5 answers
7k views
ThreadStart with Parameter method
I am trying to learn Threading in .Net. Many of you must have seen this: private void button1_Click(object sender, EventArgs e) { Thread t = new Thread(new ThreadStart(loop)); t.Start(); } ...
3 votes
4 answers
1k views
Why does c# Linq use reference instead of value in List containing List
I'm having problems with this simple code and I don't understand why c# behaves this way. The problem seems to be that c# uses the Linq expression reference instead of the value when using Lists ...