什么是闭包?我们在。net中有吗?
如果它们确实存在于。net中,你能提供一个代码片段(最好是c#)来解释它吗?
什么是闭包?我们在。net中有吗?
如果它们确实存在于。net中,你能提供一个代码片段(最好是c#)来解释它吗?
当前回答
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
闭包是传递到创建闭包的函数外部的匿名函数。 它维护它所使用的函数中的任何变量。
其他回答
闭包是保留原始作用域变量值的函数值。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主要是使用闭包构建的。对于大多数开发人员来说,最直接的方法是向动态创建的控件添加事件处理程序——当控件实例化时,您可以使用闭包来添加行为,而不是将数据存储在其他地方。
这是c# 7.0概要书中一个简单易懂的答案。
您应该知道的先决条件:lambda表达式可以引用方法的局部变量和参数 其中它被定义(外部变量)。
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
}
实部分:lambda表达式引用的外部变量称为捕获变量。捕获变量的lambda表达式称为闭包。
最后需要注意的一点:被捕获的变量是在委托实际调用时计算的,而不是在变量被捕获时计算的:
int factor = 2;
Func<int, int> multiplier = n => n * factor;
factor = 10;
Console.WriteLine (multiplier (3)); // 30
闭包是在另一个函数(或方法)中定义的函数,它使用父方法中的变量。这种位于方法中并包装在其中定义的函数中的变量的使用称为闭包。
Mark Seemann在他的博客文章中有一些关于闭包的有趣例子,他将面向对象编程和函数式编程进行了比较。
让它更详细
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.
我有一篇关于这个主题的文章。(书中有很多例子。)
从本质上讲,闭包是一段可以在以后执行的代码,但它维护了它第一次创建时的环境——即它仍然可以使用创建它的方法的局部变量等,即使该方法已经执行完毕。
闭包的一般特性是通过匿名方法和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本身已经完成。
下面是我从JavaScript中类似的代码创建的c#人为示例:
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; };
}
所以,这里有一些代码,展示了如何使用上面的代码…
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
希望这对大家有所帮助。