What are 'closures' in .NET? -
what closure? have them in .net?
if exist in .net, please provide code snippet (preferably in c#) explaining it?
edit: went through jon skeet's article understand closures , how use them in .net.
i have an article on topic. (it has lots of examples.)
in essence, closure block of code can executed @ later time, maintains environment in first created - i.e. can still use local variables etc of method created it, after method has finished executing.
the general feature of closures implemented in c# anonymous methods , lambda expressions.
here's example using anonymous method:
using system; class test { static void main() { action action = createaction(); action(); action(); } static action createaction() { int counter = 0; return delegate { // yes, done in 1 statement; // clearer this. counter++; console.writeline("counter={0}", counter); }; } }
output:
counter=1 counter=2
here can see action returned createaction still has access counter variable, , can indeed increment it, though createaction has finished.
Comments
Post a Comment