I am currently looking at lambda expression and the word closure keeps coming. Can someone explain it to me in real simple language.
I'd say this is a duplicate of: What are ‘closures’ in .NET?
"In essence, a closure is a block of code which can be executed at a later time, but which maintains the environment in which it was first created - i.e. it can still use the local variables etc of the method which created it, even after that method has finished executing."
Your shoes are in the hall; your jacket is in the kitchen. Put them on, and your gloves (they're in the drawer), when going outside.
Now you can go playing with your cars. At eleven o'clock you must go buy some bread in the corner store.
Kid plays. Forgets all the world.
Alarm clock goes off; kid sees: eleven o'clock! Oh - go outside to buy bread using the "going outside" closure.
If you really need to keep it simple, then a closure is a function with its context. The function in the closure can still access the same variables it could when it was defined, no matter where you call it from. (In Lua, these are called upvalues, which I think is a very descriptive term.)
I met the concept first in Lua, and this definition helped me understand the concept. Maybe have a look at Lua: its simpleness and power is fascinating, and certainly helps to develop a certain view at other languages. Its concept of closures would be a good example to that.
http://en.wikipedia.org/wiki/Closure%5F%28computer%5Fscience) says:
In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.
Translation:
closures close/attach the variables around the function, so that that function can be teleported to somewhere else and still use those variables
e.g. suppose you are teleported to a remote location but have still access to your coffed mug lying on your table
example:
function makefunc(x)
{
return function(){return x}
}
now using makefunc, you can make many anonymous functions which will return what you pass to makefunc so if you want a function which returns 10, use makefunc(10)() , though pretty useless way toget back 10 :)
When you know how to do something in general, you can specify some (or all) details and get a closure.
For example, you know how to buy ice-cream. Yyou know what to do if you will be in front of any shop. But if you want to go to a particular shop (for example, due to a Sunday discount), you move out of house with the aim of buying ice-cream there. "Buy some ice-cream at a store on the corner" is a closure of "buy some ice-cream". In fact, all these are closures of "buy some ice-cream somewhere":
- Buy some ice-cream at the corner
- Buy two ice-creams
- Buy two ice-creams at the corner
Now go play with your friends, son! (and I bear in mind not say anything like that in front of the children)
If the 5 year old knew C#, I would explain with this code sample:
int i = 0;
string result = null;
Action iExaminer = () =>
{
result = i % 2 == 1 ? "Odd" : "Even";
};
i = 1;
iExaminer();
Console.WriteLine(result);
If the 5 year old was learning linq, I would explain with this code sample:
string name = null;
IEnumerable<Customer> query = Customers.Where(c => c.Name == name);
name = "Bob";
// query is resolved when enumerated (which is now)
// Where will now call our anonymous method.
foreach(var customer in query)
{
Console.WriteLine(customer.Name);
}
I like the Google example for Javascript (you can morph it for C# easily). It's not something a 5 year old would understand but then I doubt an average 5 year old would understand what a function was.
/*
* When a function is defined in another function and it
* has access to the outer function's context even after
* the outer function returns
* An important concept to learn in Javascript
*/
function outerFunction(someNum) {
var someString = 'Hai!';
var content = document.getElementById('content');
function innerFunction() {
content.innerHTML = someNum + ': ' + someString;
content = null; // IE memory leak for DOM reference
}
innerFunction();
}
Take this box of legos; build yourself a nice little space craft. When you go to billy's house and bring your space craft there; with closures you can still can use all the pieces in your box of legos, even though the box was left in your bedroom.