C#中的“闭包”是什么? [英] What are 'closures' in C#?
C#中的关闭内联代表/ 匿名方法的形式a>。 闭包附加到其父方法,意味着父项方法体中定义的变量可以从匿名方法中引用。在这里有一个伟大的博客。
示例
public Person FindById (int id)
{
return this.Find(delegate(Person p)
{
return(p.Id == id);
}
}
您还可以查看 Martin Fowler 或 Jon Skeet 博客。我相信你将能够从他们中的至少一个得到更多的深度细分。
Duplicate
What are closures in C#?
解决方案
A closure in C# takes the form of an in-line delegate/anonymous method. A closure is attached to its parent method meaning that variables defined in parent's method body can be referenced from within the anonymous method. There is a great Blog Post here about it.
Example
public Person FindById(int id)
{
return this.Find(delegate(Person p)
{
return (p.Id == id);
});
}
You could also take a look at Martin Fowler or Jon Skeet blogs. I am sure you will be able to get a more "In Depth" breakdown from at least one of them....
这篇关于C#中的“闭包”是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文