What are 'closures' in .NET?

2018-12-31 03:33发布

What is a closure? Do we have them in .NET?


If they do exist in .NET, could you please provide a code snippet (preferably in C#) explaining it?


EDIT: I went through Jon Skeet's article to understand what closures are and how to use them in .NET.

标签: .net closures
12条回答
梦该遗忘
2楼-- · 2018-12-31 04:06

I have an article on this very topic. (It has lots of examples.)

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.

The general feature of closures is implemented in C# by anonymous methods and lambda expressions.

Here's an example using an anonymous method:

using System;

class Test
{
    static void Main()
    {
        Action action = CreateAction();
        action();
        action();
    }

    static Action CreateAction()
    {
        int counter = 0;
        return delegate
        {
            // Yes, it could be done in one statement; 
            // but it is clearer like this.
            counter++;
            Console.WriteLine("counter={0}", counter);
        };
    }
}

Output:

counter=1
counter=2

Here we can see that the action returned by CreateAction still has access to the counter variable, and can indeed increment it, even though CreateAction itself has finished.

查看更多
梦该遗忘
3楼-- · 2018-12-31 04:06

Basically closure is a block of code that you can pass as an argument to a function. C# supports closures in form of anonymous delegates.

Here is a simple example:
List.Find method can accept and execute piece of code (closure) to find list's item.

// Passing a block of code as a function argument
List<int> ints = new List<int> {1, 2, 3};
ints.Find(delegate(int value) { return value == 1; });

Using C#3.0 syntax we can write this as:

ints.Find(value => value == 1);
查看更多
余欢
4楼-- · 2018-12-31 04:06

A closure is when a function is defined inside another function (or method) and it uses the variables from the parent method. This use of variables which are located in a method and wrapped in a function defined within it, is called a closure.

Mark Seemann has some interesting examples of closures in his blog post where he does a paralel between oop and functional programming.

And to make it more detailed

var workingDirectory = new DirectoryInfo(Environment.CurrentDirectory);//when this variable
Func<int, string> read = id =>
    {
        var path = Path.Combine(workingDirectory.FullName, id + ".txt");//is used inside this function
        return File.ReadAllText(path);
    };//the entire process is called a closure.
查看更多
人间绝色
5楼-- · 2018-12-31 04:10

Just out of the blue,a simple and more understanding answer from the book C# 7.0 nutshell.

Pre-requisit you should know :A lambda expression can reference the local variables and parameters of the method in which it’s defined (outer variables).

    static void Main()
    {
    int factor = 2;
   //Here factor is the variable that takes part in lambda expression.
    Func<int, int> multiplier = n => n * factor;
    Console.WriteLine (multiplier (3)); // 6
    }

Real part:Outer variables referenced by a lambda expression are called captured variables. A lambda expression that captures variables is called a closure.

Last Point to be noted:Captured variables are evaluated when the delegate is actually invoked, not when the variables were captured:

int factor = 2;
Func<int, int> multiplier = n => n * factor;
factor = 10;
Console.WriteLine (multiplier (3)); // 30
查看更多
步步皆殇っ
6楼-- · 2018-12-31 04:11

If you are interested in seeing how C# implements Closure read "I know the answer (its 42) blog"

The compiler generates a class in the background to encapsulate the anoymous method and the variable j

[CompilerGenerated]
private sealed class <>c__DisplayClass2
{
    public <>c__DisplayClass2();
    public void <fillFunc>b__0()
    {
       Console.Write("{0} ", this.j);
    }
    public int j;
}

for the function:

static void fillFunc(int count) {
    for (int i = 0; i < count; i++)
    {
        int j = i;
        funcArr[i] = delegate()
                     {
                         Console.Write("{0} ", j);
                     };
    } 
}

Turning it into:

private static void fillFunc(int count)
{
    for (int i = 0; i < count; i++)
    {
        Program.<>c__DisplayClass1 class1 = new Program.<>c__DisplayClass1();
        class1.j = i;
        Program.funcArr[i] = new Func(class1.<fillFunc>b__0);
    }
}
查看更多
公子世无双
7楼-- · 2018-12-31 04:11

Here is a contrived example for C# which I created from similar code in JavaScript:

public delegate T Iterator<T>() where T : class;

public Iterator<T> CreateIterator<T>(IList<T> x) where T : class
{
        var i = 0; 
        return delegate { return (i < x.Count) ? x[i++] : null; };
}

So, here is some code that shows how to use the above code...

var iterator = CreateIterator(new string[3] { "Foo", "Bar", "Baz"});

// So, although CreateIterator() has been called and returned, the variable 
// "i" within CreateIterator() will live on because of a closure created 
// within that method, so that every time the anonymous delegate returned 
// from it is called (by calling iterator()) it's value will increment.

string currentString;    
currentString = iterator(); // currentString is now "Foo"
currentString = iterator(); // currentString is now "Bar"
currentString = iterator(); // currentString is now "Baz"
currentString = iterator(); // currentString is now null

Hope that is somewhat helpful.

查看更多
登录 后发表回答