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