Questions tagged [captured-variable]

13 questions
280
votes
10 answers

Captured variable in a loop in C#

I met an interesting issue about C#. I have code like below. List> actions = new List>(); int variable = 0; while (variable < 5) { actions.Add(() => variable * 2); ++ variable; } foreach (var act in actions) { …
Morgan Cheng
  • 73,950
  • 66
  • 171
  • 230
7
votes
4 answers

How to avoid captured variables?

I'm having a problem with foreach(var category in categories) { foreach(var word in words) { var waitCallback = new WaitCallback(state => { DoSomething(word, category); }); …
Jader Dias
  • 88,211
  • 155
  • 421
  • 625
5
votes
1 answer

Captured variables in ParameterizedThreadStart

I have the following code that creates 10 threads which in turn write out messages to the console: for (int i = 0; i < 10; i++) { { Thread thread = new Thread((threadNumber) => { for (int j = 0; j < 10; j++) …
Maverick
  • 75
  • 7
5
votes
1 answer

Captured Variables... What does that 'Captured' stands for actually?

In "Captured Variables" how a variable is captured? What does that 'Captured' term stands for actually? Does it mean referencing a value type without getting the boxing involved? Thanks
pencilCake
  • 51,323
  • 85
  • 226
  • 363
3
votes
1 answer

Swift capture value with same name

In a closure can we use the same name somehow inside as well as a value that is captured by the closure. func load(withResource resource: Resource) { var data: A? var error: Error? load(resource: resource) { (result, error) in …
richy
  • 2,716
  • 1
  • 33
  • 42
2
votes
1 answer

Combine two regex groups into a key/value pair object?

Let's say that I have the following string Type="Category" Position="Top" Child="3" ABC="XYZ".... And 2 regex groups: Key and Value Key: "Type", "Position", "Child",... Value: "Category", "Top", "3",... How can we combine these 2 captured groups…
ByulTaeng
  • 1,269
  • 1
  • 21
  • 40
2
votes
1 answer

Where is stored captured variable in Java?

I'm trying to understand concept of captured variable in Java. I found quite detailed article about it: http://www.devcodenote.com/2015/04/variable-capture-in-java.html and I'm not sure about bytecode part: Similarly, for accessing local variables…
2
votes
1 answer

Captured variables in a thread in a loop in C#, what is the solution?

I came across this example that demonstrates the case of Captured Variables within a Thread and a loop : Code 1 for (int i = 0; i < 10; i++) { new Thread(() => Console.Write(i)).Start(); } Result 1 0223558779 The suggested solution is said to be…
AymenDaoudi
  • 7,811
  • 9
  • 52
  • 84
1
vote
3 answers

Captured variable instantiating problem

I'm currently musing about some idea I can't get right. The problem is that I want to use one lambda function to instantiate a captured variable and another lambda to access a property of that variable. Since the instantiating happens within the…
VVS
  • 19,405
  • 5
  • 46
  • 65
1
vote
4 answers

Capture Variable Into An EventHandler

I might be overthinking this one a little but I could use some help in identifying a way/the best way to do the following. I have an event handler that is attached to an object that is a property of another class. In my event handler I need…
Craig Suchanec
  • 10,474
  • 3
  • 31
  • 39
0
votes
0 answers

Why can't I see captured variable behavior here?

I encountered this strange behavior with captured variables. Below are two loops. I would expect both to behave the same. Why it behaves differently? private static void Loop1() { var actions = new List(); foreach…
Hunter
  • 2,370
  • 2
  • 20
  • 24
0
votes
3 answers

How to compute rank of IEnumerable and store it in type T

I want to compute rank of element in an IEnumerable list and assign it to the member. But below code works only when called 1st time. 2nd time call starts from last rank value. So instead of output 012 and 012, I'm getting 012 and 345 class…
Ankush
  • 2,454
  • 2
  • 21
  • 27
-1
votes
1 answer

Two method overloads, async and sync lambda parameter, one implementation, no captured arguments. Is it possible?

I have a method LoopAsync that takes a lambda parameter, and invokes this lambda repeatedly a number of times with a delay. Both the method and the lambda are asynchronous: static async Task LoopAsync(Func action, int start, int…