Strange code example (teaser)
-
I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?
using System;
public class Class1 {
private Action _someAction;public void Method() { var obj1 = new object(); var obj2 = new object(); \_someAction += () => { Console.WriteLine(obj1); Console.WriteLine(obj2); }; // "Implicitly captured closure: obj2" \_someAction += () => { Console.WriteLine(obj1); }; }
}
This should be easy to figure out.
-
I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?
using System;
public class Class1 {
private Action _someAction;public void Method() { var obj1 = new object(); var obj2 = new object(); \_someAction += () => { Console.WriteLine(obj1); Console.WriteLine(obj2); }; // "Implicitly captured closure: obj2" \_someAction += () => { Console.WriteLine(obj1); }; }
}
This should be easy to figure out.
-
Am I missing something? Besides the erroneous comment (obj1 is captured, not obj2) I see no issues :doh:
Not sure what that code's trying to do, but the delegate gets both functions added to its invocation list, and this is repeated each time the containing method is called.
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com
-
Not sure what that code's trying to do, but the delegate gets both functions added to its invocation list, and this is repeated each time the containing method is called.
Nish Nishant Consultant Software Architect Ganymede Software Solutions LLC www.ganymedesoftwaresolutions.com
-
I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?
using System;
public class Class1 {
private Action _someAction;public void Method() { var obj1 = new object(); var obj2 = new object(); \_someAction += () => { Console.WriteLine(obj1); Console.WriteLine(obj2); }; // "Implicitly captured closure: obj2" \_someAction += () => { Console.WriteLine(obj1); }; }
}
This should be easy to figure out.
-
I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?
using System;
public class Class1 {
private Action _someAction;public void Method() { var obj1 = new object(); var obj2 = new object(); \_someAction += () => { Console.WriteLine(obj1); Console.WriteLine(obj2); }; // "Implicitly captured closure: obj2" \_someAction += () => { Console.WriteLine(obj1); }; }
}
This should be easy to figure out.
James Lonero wrote:
Can you spot the problem(s)?
Yes. 1) Somebody actually wrote it. 2) See (1).
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay... AntiTwitter: @DalekDave is now a follower!
-
I saw this in a code sample on a JetBrains web page. Can you spot the problem(s)?
using System;
public class Class1 {
private Action _someAction;public void Method() { var obj1 = new object(); var obj2 = new object(); \_someAction += () => { Console.WriteLine(obj1); Console.WriteLine(obj2); }; // "Implicitly captured closure: obj2" \_someAction += () => { Console.WriteLine(obj1); }; }
}
This should be easy to figure out.
And what invokes the action?
This space for rent