codethinked (kōdthĭngked) adj. To be consumed by or obsessed with code.

C# Closures Explained

Thanks to all of the functional goodness that was put into C# 3.0 we have a ton of power at our disposal. Unfortunately with a lot of this power also comes increased complexity. Programmers usually exacerbate the complexity problem, because unfortunately we often like to sound like we are smarter than we actually are.

We often like to use big words and lots of acronyms to scare away the neophytes. Oh crap, there I go again…

Anyways, one of these topics that is blindingly simple is closures. But I continue to see definitions that are as clear as mud to the average developer. Let’s go ahead and look at the Wikipedia definition of a closure:

“In computer science, a closure is a first-class function with free variables that are bound in the lexical environment.”

All clear, right? Well, if it is for you, then super… you can stop reading. But if not, and the next time this topic comes up you want to sound like Super Duper Computer Science Guy™ ... then keep reading.

Sir, Your Functions Are First Class

So first, what is a "first-class function"? A first class function simply means that it is a function which your language treats as a first class data type. It means that you can assign a function to a variable, pass it around, and invoke it... just like a normal function. In C# we can create a first class function using anonymous methods:

Func<string,string> myFunc = delegate(string var1)
                                {
                                    return "some value";   
                                };

Or we can do it using lambdas:

Func<string,string> myFunc = var1 => "some value";

Both of those are functionally equivalent, and they just create a method that takes a string and returns a string. We can call that method by invoking the variable just like we would any method:

string myVar = myFunc("something");

This means that C# supports first class functions, yay!

I Like My Variables Free

And so now we have first-class functions with free variables... And what, pray tell, is a free variable? A free variable just happens to be a variable which is referenced in a function which is not a parameter of the function or a local variable of the function. Okay, so it might look like this:

var myVar = "this is good";

Func<string,string> myFunc = delegate(string var1)
                                {
                                    return var1 + myVar;   
                                };

Okay, so the anonymous delegate is referencing a variable that is in its enclosing scope. That variable isn't a parameter, and it isn't a local variable. So it is a free variable. So what?

It Has To Close Over It, Son

So, what happens in this case:

static void Main(string[] args)
{
    var inc = GetAFunc();
    Console.WriteLine(inc(5));
    Console.WriteLine(inc(6));
}

public static Func<int,int> GetAFunc()
{
    var myVar = 1;
    Func<int, int> inc = delegate(int var1)
                            {
                                myVar = myVar + 1;
                                return var1 + myVar;
                            };
    return inc;
}

Hmmm, stare at that for just a second. When we call "GetAFunc", we get a method back that increments a local variable inside of the method. You see? "myVar" is a local variable, but when we return the "inc" method, it is bound inside of the delegate.

But don't local variables get created on the stack? Don't they go away when we finish executing the method? Normally yes. But if we ran this code, this would be the result:

 image

So, when we passed back the method, the variable now lives along with the method. Outside of its original scope. You see, it got incremented when we called the method twice. Crazy!

But you know what is even more crazy? You just learned what a closure is! You just bound some free variables in the lexical environment! Don't you just feel super smart now?

You see, it is oh so simple. It is really all about variables getting referenced which might leave scope. So you can say that these delegates are "closed" over these variables, which causes them to live outside of their lexical scope.

How Does It Work?

Well, the next question I always ask is, how does it work. And normally when people ask me this question I say "very carefully", but that is only because I am annoying. In reality this is implemented in a more straightforward manner than you might think. What we are really doing here is binding up a method with some data and passing it around. Geez, I sure do wish we had something like that in C#... oh wait, we do, it is called a class!

You see, the C# compiler detects when a delegate forms a closure which is passed out of the current scope and it promotes the delegate, and the associated local variables into a compiler generated class. This way, it simply needs a bit of compiler trickery to pass around an instance of the compiler generated class, so each time we invoke the delegate we are actually calling the method on this class. Once we are no longer holding a reference to this delegate, the class can be garbage collected and it all works exactly as it is supposed to!

Easy Peasy

Now, the C# compiler team would probably spit in my face for simplifying it this much, but at least at the conceptual level it is a fairly straightforward process. My head hurts a little bit thinking of all of the complex interactions and edge cases that would accompany something like this though.

Comments

Quooston

Mr Etheredge, you humble me sir. Beautifully explained, many thanks.


Quooston

June 22. 2010 07:47

Australia
Josh

what an interesting concept! actually makes me miss college and all this crazy computer sciencey stuff was still new. thanks for the insight!

Josh

June 22. 2010 10:21

United States
Andy H

Thanks for the writeup.

I've got a couple questions, could you expand a bit on what "lexical environment" means?

Also, what are some examples/ideas, in the C# world, where it would make good sense to use a closure?

Andy H

June 22. 2010 10:22

United States
Tom

Excellent!  That's one less thing off of my "things to learn when I have the time" list!

Tom

June 22. 2010 11:21

United States
Hash

Pretty nice explanation. Thank You.

Hash

June 22. 2010 11:40

India
Jonathan Pryor

@Andy H: A "lexical environment" means "scope".

As for where it would make sense, that depends on what you're doing. Smile

A cursory grep of some sources I have shows this use of closures:


  var data = new int [7];
  var result = new int [] { 6, 7, 8, 9, 10, 11, 12 };

  int j = 0;
  6.UpTo (12).ForEach (i => { data [j++] = i; });

  AssertAreSame (result, data);

This is to test an Int32.UpTo(int, int) extension method, and uses an IEnumerable<T>.ForEach(Action<T>) extension method for the testing.  Since we can't provide additional parameters to .ForEach(), we rely on a closure over the 'data' and 'j' variables, allowing for `data[j++]=i`, where `i` is a parameter to the lambda.

A simpler (but likely sillier) example would be this:

  Func<int, int> IncrementBy (int by)
  {
    return value => value + by;
  }

In the above, `by` is captured in a closure, allowing:

  Func<int, int> inc1 = IncrementBy (1);
  Console.WriteLine (inc1 (2)); // == 3
  Func<int, int> inc9 = IncrementBy (9);
  Console.WriteLine (inc9 (2)); // == 11

The primary motivation for closures is that it allows for more succinct code, as you need to modify fewer "intermediate" layers to provide additional "contextual" data; consider a Linq-to-SQL query that does name matching:

  IQueryable<Person> GetManagersWithName (IQueryable<Person> people, string name)
  {
    return  from p in people
      where p.Name == name &&  // implicit closure over 'name'
            p.PersonType == PersonType.Manager
      select p;
  }

Without closures, we'd either need to copy&paste the entire body into each code site (yay maintenance! so much for DRY!) or complicate the calling code.  (There are times in C++ where, since I couldn't use a closure, I had to add additional member variables to a class and change the argument lists of multiple methods so that the contextual data I needed was "passed down" through all the intermediate methods.  It's NOT FUN.)

Jonathan Pryor

June 22. 2010 11:54

United States
James Michael Hare

Very wonderfully and simply explained!  Love it!  I'm going to forward to the folks in our C# teams where I work as there's a lot of folks who scratch their heads at closure and I think this may be just the thing to help!

James Michael Hare

June 22. 2010 12:11

United States
Buckley

Excellent. Couldn't have said it myself better Smile

Buckley

June 22. 2010 16:27

Belgium
Sean Kearon

What an elegant description!

Sean Kearon

June 22. 2010 17:45

United Kingdom
Avi Shilon

Thank you both Justin and Jonathan for the wonderful tips.

Avi Shilon

June 23. 2010 05:32

Israel
James

Excellent writeup. Like your style.

James

June 23. 2010 08:01

United Kingdom
Cog

I read a lot of programming blogs/websites.  This is one of the best posts on anything I've ever read.  Heading to the other parts of the site now...

Cog

June 23. 2010 10:13

United States
kinghajj

Here's a nice use for closures I found a few days ago: resource cleanup at exit.

var excel = new Excel.Application() { Visible = false };
AppDomain.CurrentDomain.ProcessExit += (o, args) => excel.Quit();

This code ensures that the Excel application is stopped as this process exits.

kinghajj

June 23. 2010 10:51

United States
Jonathan Allen

Closures were added in version C# 2. It was VB that didn't get them until .NET 3.

http://joe.truemesh.com/blog/000390.html

Jonathan Allen

June 23. 2010 14:29

United States
Dimiter "malkia" Stanev

Another example for using closures is to do Undo/Redo operations.

For example in an Editor, you can have a function DeleteLines() that would delete all lines in a given range, but the function would return to you a closure, that you can call later to restore the state. Same for InsertLines() - it would return you back a function that would restore the state.

All you have to do then, is just keep a list of function pointers (no arguments, no extra data) - everything else is "hidden".

It's not the most efficient way for undo/redo, but this gives you some prototyping powers, where the performance/memory are not that important, but feature completeness is.

Here is an example in Lua: (Sorry, I don't have much experience in C#)
http://gist.github.com/428272

Also look in google for "undo using closures"

Obviously there are many many other good examples.

Dimiter "malkia" Stanev

June 23. 2010 15:52

United States
Justin Etheredge

@Jonathan Thanks, I'm aware, when I said function programming goodness I was referring to all of the additional functionality we got with C# 3.0 which makes it more often used. I don't know about you, but I never really saw people using anonymous methods very often. Smile

But anyways, you are correct, anonymous methods were added in C# 2.0 and you can create closures with them.

Justin Etheredge

June 23. 2010 15:58

United States
Justin Etheredge

@Dimiter Excellent example, thanks!

Justin Etheredge

June 23. 2010 15:58

United States
abc

please try following to examples, the behavior of "close over" the free variable is inconsistent:

============================================

   class Program
    {
        static void Main(string[] args)
        {
            var multiple = 2;
            
            Func<int, int> fun_double = GetAFunc(multiple);

            multiple = 3;

            Func<int, int> fun_triple = GetAFunc(multiple);

            Console.WriteLine(fun_double(5));
            Console.WriteLine(fun_triple(5));
            return;
        }

        
        public static  Func<int,int> GetAFunc(int multiple)
        {
            Func<int, int> functor = delegate(int var1)
            {
                return var1 * multiple;
            };
            return  functor;
         }

    }



===============================================


   class Program
    {
        static void Main(string[] args)
        {
            var multiple = 2;
            
            Func<int, int> fun_double = delegate(int var1)
            {
                return var1 * multiple;
            };
      
            multiple = 3;

            Func<int, int> fun_triple = delegate(int var1)
            {
                return var1 * multiple;
            };

            Console.WriteLine(fun_double(5));
            Console.WriteLine(fun_triple(5));
            return;
        }

    

abc

June 24. 2010 05:22

United States
Justin Etheredge

@abc I wouldn't say that is inconsistent. In the first example you are closing over the parameter to the method. So you are creating two closures which are binding to two different instances of a value type. So they are getting "bound" separately. I'm pretty sure that if you were using a mutable reference type, you would get the behavior you are expecting, but I'm not sure I'd say it is inconsistent. It is leveraging the model that .net has always used.

Justin Etheredge

June 24. 2010 07:03

United States
Nikolay

This is the first explanation of closures I've read that actually make sense Smile
Thank you so much, I am, for the first time, confident that I get the concept (bow)

Nikolay

June 24. 2010 19:31

United States
Sarah

Before understand I got it:

            int n = 1;
            var f = new Func<int, int>(a => a += (n += 1));
            Console.WriteLine(f(5));
            Console.WriteLine(f(6));

that do the same Smile

Sarah

June 29. 2010 16:17

Brazil
Nobody

Short version:

            int n = 1;
            var f = new Func<int, int>(a => a += (n += 1));
            Console.WriteLine(f(5));
            Console.WriteLine(f(6));

Nobody

June 29. 2010 16:41

Brazil
Shiggity

This is the best explanation of this I've ever read

Shiggity

July 26. 2010 19:07

United States

Add Comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading