Python for .NET Developers Transcripts
Chapter: The Python Language
Lecture: C# closures

Login or purchase this course to watch this video and the rest of the course contents.
0:00 I want to talk about another function feature of C# that's pretty interesting. I suspect it's not used that often.
0:08 It's a little more common in the Javascript space but it's definitely a feature of C#. It's really interesting. It's called closures.
0:15 So the idea of a closure is I've got some function going to pass some data to it. And then that functions going to create another function here.
0:27 And that other function is going to capture or get the closure of these variables that were ambient to it.
0:35 Kind of like globals but only within this function which was only defined temporarily. Then when you execute that function later
0:43 we're going to return it. When we execute it later its actually going to remember these. It's just like we went and created a counter
0:50 little method does counting. We created a counter object a Counter class and gave it two member variables. Where does it start? Actually 3.
0:58 What is its start value and its counter ID. And then each time you call execute or something on it it can work with those values.
1:05 Same thing here except for not creating a class. It's just a function. So notice this delegate that we're creating has it's void, it has no arguments.
1:14 And yet it's working with start which is defined right here. So we're working with counter idea which is defined there. And its working with starterVal
1:22 which comes in here like this. So we're going to call this CreateCounter. It's going to describe what it's doing creating counter with this.
1:29 Create a function that does not execute. We're just handing it back. But it's now captured that state that is erased when the function returns.
1:36 So it hangs on to it really funky. So we're going to call CreateCounter with 7 and then an id of 1 and CreateCounter with -100 and an id of 2.
1:46 We call counter1() it goes and increments 7 to 8. And we call it again down here it goes 8 to 9, 9 to 10. But this one called with different values
1:56 actually has different like state captured in it. It's different closures. So it'll be like -100 to -99 and so on.
2:03 So let's just run this and see what we get. Check this out this is crazy. I called a function pass it some arguments
2:09 and then I called it again and again each time being void and yet it remembers that it's id was 1 it's start value was 7 and it's current value is 10.
2:18 First 8, 9, and then 10 it holds on to this. It's a really interesting idea of how to pass additional data.
2:25 It might seem crazy like why would I ever use this. Well, imagine you have a lambda expression and your trying to do a sort and you need to use data.
2:32 Its ambient to the current scope but there's no way within that lambda function to pass it over like your passing it through the list
2:40 and the list only passes what it passes. So you can use closure or this capture stuff to actually get additional information or values
2:48 into these lambda expressions. Alright, really really cool stuff. This is how you do it in C#. All you do is you have a function
2:57 a delegate in this case could be a lambda expression as well. And all you have to do is just use the values
3:03 here with you know that come from the outside use them inside and now their captured and held onto forever.
3:09 And they can even you saw they can be changed right? Like the start keeps getting changed and changed and changed to remembered between calls
3:19 up here. So that's closure in C#

Talk Python's Mastodon Michael Kennedy's Mastodon