什么是闭包?我们在。net中有吗?
如果它们确实存在于。net中,你能提供一个代码片段(最好是c#)来解释它吗?
什么是闭包?我们在。net中有吗?
如果它们确实存在于。net中,你能提供一个代码片段(最好是c#)来解释它吗?
当前回答
Closures are chunks of code that reference a variable outside themselves, (from below them on the stack), that might be called or executed later, (like when an event or delegate is defined, and could get called at some indefinite future point in time)... Because the outside variable that the chunk of code references may gone out of scope (and would otherwise have been lost), the fact that it is referenced by the chunk of code (called a closure) tells the runtime to "hold" that variable in scope until it is no longer needed by the closure chunk of code...
其他回答
如果你有兴趣了解c#如何实现闭包,请阅读“I know the answer (its 42) blog”。
编译器在后台生成一个类来封装匿名方法和变量j
[CompilerGenerated]
private sealed class <>c__DisplayClass2
{
public <>c__DisplayClass2();
public void <fillFunc>b__0()
{
Console.Write("{0} ", this.j);
}
public int j;
}
对于函数:
static void fillFunc(int count) {
for (int i = 0; i < count; i++)
{
int j = i;
funcArr[i] = delegate()
{
Console.Write("{0} ", j);
};
}
}
把它变成:
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);
}
}
闭包是保留原始作用域变量值的函数值。c#可以以匿名委托的形式使用它们。
举一个非常简单的例子,看看下面的c#代码:
delegate int testDel();
static void Main(string[] args)
{
int foo = 4;
testDel myClosure = delegate()
{
return foo;
};
int bar = myClosure();
}
在它的末尾,bar将被设置为4,并且myClosure委托可以传递给程序的其他地方使用。
闭包可以用于很多有用的事情,比如延迟执行或简化接口——LINQ主要是使用闭包构建的。对于大多数开发人员来说,最直接的方法是向动态创建的控件添加事件处理程序——当控件实例化时,您可以使用闭包来添加行为,而不是将数据存储在其他地方。
我有一篇关于这个主题的文章。(书中有很多例子。)
从本质上讲,闭包是一段可以在以后执行的代码,但它维护了它第一次创建时的环境——即它仍然可以使用创建它的方法的局部变量等,即使该方法已经执行完毕。
闭包的一般特性是通过匿名方法和lambda表达式在c#中实现的。
下面是一个使用匿名方法的例子:
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);
};
}
}
输出:
counter=1
counter=2
在这里,我们可以看到CreateAction返回的操作仍然可以访问counter变量,并且确实可以增加它,即使CreateAction本身已经完成。
基本上闭包是一段代码,你可以把它作为参数传递给函数。c#支持匿名委托形式的闭包。
这里有一个简单的例子: 列表。Find方法可以接受并执行一段代码(闭包)来查找列表的项。
// 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; });
使用c# 3.0语法,我们可以这样写:
ints.Find(value => value == 1);
Func<int, int> GetMultiplier(int a)
{
return delegate(int b) { return a * b; } ;
}
//...
var fn2 = GetMultiplier(2);
var fn3 = GetMultiplier(3);
Console.WriteLine(fn2(2)); //outputs 4
Console.WriteLine(fn2(3)); //outputs 6
Console.WriteLine(fn3(2)); //outputs 6
Console.WriteLine(fn3(3)); //outputs 9
闭包是传递到创建闭包的函数外部的匿名函数。 它维护它所使用的函数中的任何变量。